Is it possible to select a selector using indexOf
how
<div id="box_1">
<div id="box_2">
<div id="box_3">
If I want to get all id starting with "box_", how can I do it like this.
$("#box_" + anything )
Unfortunately, wrapping a div will not work, because it will get all other divs aside and between divs.
I think I can give them another class and refer to it like that, but just wondering if there is something like this there .. thanks.
+5
5 answers
Perhaps the attribute starts with a selector, as others have mentioned, but it might be better to give each element a class:
<div id="box_1" class="box"></div>
<div id="box_2" class="box"></div>
<div id="box_3" class="box"></div>
:
$(".box")
+2
You can use attribute selector :
$('[id^="box_"')
This will give you all the elements, the id starts with "box_". If you need, qualify it with the element:
$('div[id^="box_"')
+1