JSFiddle - React, Tailwind, and code Playground
HTML
<div class="elem">Click here</div>
<div class="elem">Click here</div>
JavaScript
function attachClickToElem() {
$('.elem').each(function () {
var $elem = $(this);
// check if event handler already exists
// if exists, skip this item and go to next item
if ($elem.data('click-init')) {
return true;
}
// flag item to prevent attaching handler again
$elem.data('click-init', true);
$elem.on('click', function () {
alert('Hello World');
});
});
}
$(function () {
attachClickToElem();
attachClickToElem();
});