jQuery - Check if the tag's content is equal to sometext then do something

xperator picture xperator · Dec 22, 2011 · Viewed 51.6k times · Source

Let's say I have many of these in my content div : <cite class="fn">blabla</cite>

How can I check every cite tag's content (in this case: blabla) with class fn to see if it equals to "sometext" then change it's color to red ?

Very simple.

Answer

powerbuoy picture powerbuoy · Dec 22, 2011
$('cite.fn:contains(blabla)').css('color', 'red');

Edit: though that will match "blablablabla" as well.

$('cite.fn').each(function () {
    if ($(this).text() == 'blabla') {
        $(this).css('color', 'red');
    }
});

That should be more accurate.

Edit: Actually, I think bazmegakapa's solution is more elegant:

$('cite.fn').filter(function () {
    return $(this).text() == 'blabla';
}).css('color', 'red');;