Skip to content
Advertisement

JS select tag following an tag containing “foo”

So I’ve got this little chrome extension project and I’m trying to figure out how to find the first “ul” element on the page that comes after an “h2” element that contains a particular word.

Example of what the web page would look like…

JavaScript

However the nature of varying page code means that it may looks more like this…

JavaScript

Or even…

JavaScript

I can get the “h2” element containing “foo” by…

JavaScript

But that is where I’m stuck, I can’t figure out how to search the rest of the DOM that follows the found “h2” tag. Sibling selectors won’t work as the “h2” and the “ul” may not be within the same element.

Also being a chrome extension rules out using anything like jQuery.

Does anyone know if this is possible? Any thoughts would be very much appreciated!

Advertisement

Answer

This should do the trick:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement