Find items using part of the identifier

I have divwhere the id looks like this_div_id_NUMBEReveryone divhas a different part NUMBER. How to find everything divwith just the this_div_idid part?

+4
source share
6 answers

you can use querySelectorAll to get partial attributes, including ID:

document.querySelectorAll("[id^='this_div_id']")

the ^ next to the equal sign means "starts with", you can use * instead, but it is prone to false positives.

, ( ) comapare querySelectorAll; jQuery , .

: :

document.querySelectorAll("[id^='this_div_id']:not([id$='_test_field'])");

not() -, "_test_field" .


/: http://pagedemos.com/partialmatch/

+10

querySelectorAll

querySelectorAll CSS HTMLNodeList ( ) , css.

css ^ ( ) . .

document.querySelectorAll("[id^='this_div_id']");

, querySelectorAll node. , dom .

, getElementsByClassName ( , querySelector).

var divs = document.getElementsByClassName("divClass");
+10

:

div[id^="this_div_id"]

+1

:

[id^="this_div_id_"]

JavaScript: ()

document.querySelectorAll('[id^="this_div_id_"]')

jQuery: ()

$('[id^="this_div_id_"]')

CSS: ()

[id^="this_div_id_"] {
    /* do your stuff */
}

?

Using [] in the selector you can select attributes. Use "=" to exactly match the value, or use "^ =" to check if the value starts. Read more about it here .

+1
source

Better to use classes. But there is one solution you need (if you are using jQuery):

$("*[id^='this_div_id']")
+1
source

Just using the attribute selector as shown below:
$("[id^='this_div_id_']")

+1
source

All Articles