I need to use pure Javascript for the first time in a long while, and having gotten used to the comfy mattress of jQuery, all the important stuff is escaping me.
I need to select a bunch of divs on regular expression. So I have stuff like this;
<div id="id_123456_7890123"> .. </div>
<div id="id_123456_1120092"> .. </div>
<div id="id_555222_1200192"> .. </div>
<div id="id_123456_9882311"> .. </div>
And I'd need to create a loop that goes through all the divs with an id that begins with id_123456_
. How would I go about doing that?
I used jQuery with the :regex
filter plugin before, but looking at it, it doesn't seem like there's much I could salvage in a pure javascript rewrite.
HTML DOM querySelectorAll() method will work here.
Borrowed from StackOverFlow here
This works by recursively traversing the whole DOM.
It's possibly not the most efficient, but should work on every browser.
See http://jsfiddle.net/alnitak/fgSph/
In plain javascript, you could do this generic search which should work in every browser:
Working example: http://jsfiddle.net/jfriend00/pYSCq/
Here you are: http://jsfiddle.net/howderek/L4z9Z/
HTML:
Javascript: