How to get regexp DOM elements in javascript?

For example, I need all the elements with the identifier "hide_" + value. This function should return "hide_1" and "hide_30", etc., Depending on the page elements.

+4
source share
3 answers

See the dollar-dollar syntax:

$$('a[id^="hide_"]') 

should be attached to you, whose identifiers begin with "hide_".

Most CSS3 is supported from Prototype 1.5.1 +.

+3
source

One option is to assign a common class to these elements and get them using

 var elements = $$('.class'); 
0
source

Pure DOM for general regex:

 var all_tags = document.getElementsByTagName("*"); var results = []; for (var i = all_tags.length-1; i >= 0; -- i) if (regex.test(all_tags[i].id)) results.push(all_tags[i]); return results; 
0
source

Source: https://habr.com/ru/post/1305566/


All Articles