You can search for many elements like this: #id1, .class1 > div, a[data-id~="42"] .
And inside the handler to work with this :
// Создаём строку вида "#block1, #block2" и т. д. let idPool = Array(...Array(4)).map((_, i) => '#block' + ++i).toString(); // Обработчик, аргумент - элемент в жуквери-обёртке const handler = e => console.info(`data-id: ${e.data('id')}, value: ${e.text()}`); $(idPool).on('click', function(){ // this - DOM-элемент, оборачиваем его в жуквери (если нужно, конечно) handler($(this)); });
div[data-id]{ padding: 5px; background: lightgreen; margin: 5px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='block1' data-id='42'>1</div> <div id='block2' data-id='24'>2</div> <div id='block3' data-id='22'>3</div> <div id='block4' data-id='44'>4</div>
Although, of course, it is more correct to combine the set of similar objects with a class.