1

I'm trying to create a calender application using jquery. I'm using a table that has a col for each date.

<td>17</td>

I want to add a class to the date that is today. Not very good with jquery though. How can I fix this?

<script type="text/javascript">
  $(document).ready(function(){
      $('td').load( function(){
          var today = new Date();
          if($(this).innerText()==  today.getDay(){
              this.addClass("today")
          }
      })
  });
</script>
1
  • Are these just random guesses? Commented Feb 18, 2012 at 2:55

2 Answers 2

3

I don't think you need the .load(). $(document).ready() should do what you need. You'll need to parse the date, too. Try this:

$(document).ready(function(){
    $("td").each(function(){
        if($(this).text().trim() == new Date().getDate()){
            $(this).addClass("today");
        }
    });
});

Edited after I saw the data that is actually in the <td>s in the OP's post.

Sign up to request clarification or add additional context in comments.

2 Comments

@jfriend00 I had this wrong initially, should be good now. I was assuming you had a whole date in the <td> instead of just the day of the month.
it only has the day of the month i.e. 18, for today.
1

To add a class, you have to use a jQuery object like this as addClass() is a jQuery method, not a DOM method:

$(this).addClass("today");

Also, it seems likely that this line of code isn't doing what you want it to:

if($(this).innerText()==  today.getDay()

First, it's missing a closing paren at the end of the line. Second, getDay() returns an integer 0 through 6 for the day of the week. Is that really what you're trying to compare to the innerText()?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.