user2421594
user2421594

Reputation: 81

Jquery finding specific indexOf

Hi I have this piece of code and what I want to happen is to, find all div "Complete!" then if it exist apply the css but only on "this" that was found and not the other ones that have a similar class, here's my code

var boxComplete = $('.profile-block p').text().indexOf('Complete!') > -1;
 if (boxComplete) {
 $('.profile-block p').parent().css('background', 'rgba(255,255,255,0.2)');
}

The issue here is that it's applying the css to all other parent divs and I only want it on the one that was found

Upvotes: 0

Views: 35

Answers (2)

charlietfl
charlietfl

Reputation: 171669

Can use :contains selector.

$('.profile-block p:contains(Complete)').parent().css('background','rgba(255,255,255,0.2)');

:contains selector API docs

Upvotes: 0

Amit Joki
Amit Joki

Reputation: 59232

You can just filter it out and then apply the css:

$('.profile-block p').filter(function() {
    return $(this).text().indexOf('Complete!') != -1;
}).parent().css('background', 'rgba(255,255,255,0.2)');

Upvotes: 2

Related Questions