zbzxcb
zbzxcb

Reputation: 5

jquery: how to ignore immediate children() and go deeper until match specified element?

$(this).parent().parent()
.children(this.attr("tagName").toLowerCase())
.css("background-color", "yellow");

xpath:

/html/body/div/table/tr/td/b/a

$(this).tagName is Anchor <a>

the problem with this is that children() looks at the immediate children of $(this).parent().parent(). Hence it will highlight <b> instead of <a>

i need a way to ignore this immediate children restriction, and select <a> not `

Upvotes: 0

Views: 662

Answers (2)

cletus
cletus

Reputation: 625467

Use find() to search all descendants:

$(this).parent().parent()
.find(this.attr("tagName").toLowerCase())
.css("background-color", "yellow");

Upvotes: 2

Daniel Worthington
Daniel Worthington

Reputation: 540

Try:

$(this).css("background-color", "yellow");

Unless you are trying to highlight all tags at the same level as "this" in which case try:

$(this).siblings("a").css("background-color", "yellow");

Upvotes: 0

Related Questions