Snickbrack Snickbrack - 1 year ago 72
HTML Question

Why does jquery not return the very last entry of DOM?

My expected Goal:

I am trying to select the very last node of a DOM Element which is covered by following regex:


This regex will cover Strings like
. So Strings with 3 times
and text in between and another 3 times
and a number in between.

My Structure:

I have a Div Element which contains many span-elements and the very last span element contains this

My current status:

And now I want to select only this span element which contains a text covered by the regex mentioned above. But JQuery selects the parent of the parent of the specific element.

Below is my JS-Search-Code:

var regex = /(\[{3})[A-Z]+(\]{3})(\[{3})([0-9]{1,3})(\.*)([0-9]*)(\]{3})/g;
var ElementsToChange_ = $("span").filter(function () {
return regex.test($(this).text());

Is there anything I am missing or missunderstanding?


Here is a Fiddle Link!

Answer Source

When you select span you get ALL spans. Because the span has the text in it that matches, just happens it is nested with another span that has the text.

Now the solutions depend on what can be in the span, but in your cause you can probably just select spans that do not have spans in them.


if it can have other elements inside such as <span>xxxx<span>foo</span></span> than it is a little more tricky. You would need to remove the children nodes and then read the text.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download