wordpress user
wordpress user

Reputation: 584

add class to closest child

This question has been asked earlier , but I am still not getting the answer
Question is if element contains text , add the class. But there are 2 elements with same class.
For Example

<div class="xyz">123</div>
<div class="add">...</div>


<div class="xyz"></div>
<div class="add">...</div>

Why doesnt this work?

if ($('div.xyz:contains("123")').length > 0) {
$(this).next(".add").addClass("long");
}

Upvotes: 2

Views: 97

Answers (2)

Viktor Kukurba
Viktor Kukurba

Reputation: 1370

Simplified version should also work

$('div.xyz:contains("123") +.add ').addClass("long");

Upvotes: 0

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

Try like this

$('div.xyz:contains("123")').each(function(){
  $(this).next(".add").addClass("long");
})

DEMO

Upvotes: 5

Related Questions