user1116214
user1116214

Reputation: 11

Show element with specific content (jquery)

Is it possible to address all li elements, which have a specific content without giving them a unique id/class? like:

<ul class="list">
    <li>test</li>
    <li>test2</li>
    <li>test</li>
</ul>

All li's with the content "test" should get a red color.

Thanks for your help!

Upvotes: 1

Views: 99

Answers (4)

Bassam Mehanni
Bassam Mehanni

Reputation: 14944

this should do the trick

$('li:contains("test")').css('color', 'red');

Upvotes: 1

Jeaf Gilbert
Jeaf Gilbert

Reputation: 12021

$(function() {
    $('.list li').each(function() {
        if ($(this).html() == 'test') {
            $(this).css('color', 'red');
        }
    });
})

Upvotes: 0

Richard Dalton
Richard Dalton

Reputation: 35803

Filter should work pretty well for you here:

$('li')
    .filter(function() { return $(this).text() === "test" })
    .css('color', 'red');

http://jsfiddle.net/infernalbadger/KQPbA/

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039588

You could use the .filter() method:

$('li').filter(function() {
    return $(this).text() === 'test';
}).css('color', 'red');

And here's a live demo.

Upvotes: 5

Related Questions