Reputation: 584
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
Reputation: 1370
Simplified version should also work
$('div.xyz:contains("123") +.add ').addClass("long");
Upvotes: 0
Reputation: 25352
Try like this
$('div.xyz:contains("123")').each(function(){
$(this).next(".add").addClass("long");
})
Upvotes: 5