If I want to get all id starting...">

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
source share
5 answers

You can use the attribute begins with a selector:

$("div[id^=box_]");
+4
source

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

:

$('div[id^="box_"]');

, ID, .

+1

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
source

If you don’t know if it starts or ends with any line, you can try a selector *=that will look within the attribute value.

$("div[id*='box_']");
+1
source

All Articles