You can use classes as a way to find groups of elements, as another poster suggested. I personally don't like to do that because I think classes should be for CSS styling, and using them with the intent of making them selectors can sometimes lead to unanticipated consequences if you decide to put some class on another element.
It's perfectly alright to add your own properties to an HTML tag and use those. Unlike id
, they don't need to have unique values. A way I might handle this would be:
<input type="checkbox" id="slider0" role="slider" checked>
<input type="checkbox" id="slider1" role="slider">
<input type="checkbox" id="slider2" role="slider" checked>
<input type="checkbox" id="slider3" role="slider" checked>
Here I've added a made-up property called role
. If I want to find out which sliders are checked, I can get an array of all the sliders and loop through them like this:
let checkedSliders = [];
$('[role="slider"]').each((idx,element)=>{
if ($(element).prop('checked')) checkedSliders.push(element);
}
You can use any HTML property other than id
for non-unique values. The point of id
is that it is intended to be totally unique on the page. Many new browsers will have a console warning if multiple tags have the same id
.