Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 334
xperator Avatar asked Dec 22 '11 12:12

xperator


People also ask

How do I check if a div contains a specific text?

To check if a div element contains specific text:Use the textContent property on the element to get the text content of the element and its descendants. Use the includes() method to check if the specific text is contained in the div . If it is, the includes() method returns true , otherwise false is returned.

How do you check a string contains a substring in jQuery?

In this case, we will use the includes() method which determines whether a string contains the specified word or a substring. If the word or substring is present in the given string, the includes() method returns true; otherwise, it returns false. The includes() method is case sensitive.

What do === mean in jQuery?

=== This is the strict equal operator and only returns a Boolean true if both the operands are equal and of the same type.

How check value is same or not in jQuery?

It's pretty simple, just do a comparison with == and the input's values. Place this inside of the submit() of your form. var match = $('#id1'). val() == $('#id2').


1 Answers

$('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');;
like image 90
powerbuoy Avatar answered Oct 08 '22 06:10

powerbuoy