Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 'contains' in an if statement?

I have HTML that looks like this:

<div class="item-list">
 <h3>Monday Sep 21</h3>
 <h3>Tuesday Sep 22</h3>
 <h3>Wednesday Sep 23</h3>

If today's date is on the list, then that date should be red. If today is not on the list (hey, it's still August!), then the 21st should be red. I used this code to successfully turn Sept 21 red, but I don't know how to put it in an if/else. [I tried some basic stuff, and searched, but I am lame with js.]

$(".item-list h3:contains('Monday Sept 21')").css('color','red');

(That "Monday Sept 21" will eventually be a variable based on today's date.)

like image 210
Eileen Avatar asked Aug 11 '09 22:08

Eileen


2 Answers

Using the is traversal method:

$(".item-list h3").each(function () {
    if ($(this).is(':contains("Monday Sept 21")')) {
        $(this).css("color", "red");
    } else {
        // do something
    }
});
like image 192
karim79 Avatar answered Oct 21 '22 19:10

karim79


Thanks to @karim79's answer, I see I can do a simple IF call directly like this:

var val = $('#customTextArea').is(':contains("Enter Text Here")');
like image 31
Gene Bo Avatar answered Oct 21 '22 18:10

Gene Bo