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.
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.
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.
=== This is the strict equal operator and only returns a Boolean true if both the operands are equal and of the same type.
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').
$('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');;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With