0

I am outputting a calendar with PHP and each of the td elemets which represents a date has the following code:

onclick="return getDate(2016 + /10/ + 5);"

This is calling a function on the page which does two things:

  1. It evaluates the passed date to see if it is greater or lesser then the current date (Y-m-d) using moment.js
  2. It adds a class of .day-selected if the date is over the current date and a class of .day-expired otherwise.

Here is the function:

function getDate(date) {

    var dateToday = moment();
    var selectedDate = moment(date);

    if (selectedDate.isBefore(dateToday, 'day')) {
        $(this).addClass('day-expired');
        return true;
    } else {
        $(this).addClass('day-selected');
        return false;
    }
}

It is not working. I want to make $(this) which is the specific td element that was clicked, have a new class when it is clicked depending on the date that is passed. Here is a snippet of what I have so far...

function getDate(date) {

    var dateToday = moment();
    var selectedDate = moment(date);
    
    if (selectedDate.isBefore(dateToday, 'day')) {
        $(this).addClass('day-expired');
        return true;
    } else {
        $(this).addClass('day-selected');
        return false;
    }
}
.day-selected {
    background:#7cd97c;
    color:#fff;
}

.day-expired {
    background:#ed5252;
}

.day {
  background:#0000ff;
  padding:30px;
  color:#fff;
}
<script src="http://momentjs.com/downloads/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
<tr>
  <td>
    <div class="day" href="" onclick="return getDate(2016 + /10/ + 2);">
      2
    </div>
  </td>
  <td>
  <div class="day" href="" onclick="return getDate(2016 + /10/ + 3);">
    3
  </div>
  </td><td id="tt-calendar-today">
  <div class="day" href="" onclick="return getDate(2016 + /10/ + 4);">
    4
  </div>
  </td><td>
  <div class="day" href="" onclick="return getDate(2016 + /10/ + 5);">
    5
  </div>
  </td><td>
  <div class="day" href="" onclick="return getDate(2016 + /10/ + 6);">
    6
  </div>
  </td><td>
  <div class="day" href="" onclick="return getDate(2016 + /10/ + 7);">
    7
  </div>
  </td><td>
  <div class="day" href="" onclick="return getDate(2016 + /10/ + 8);">
    8
  </div>
  </td>
</tr>
</table>
Jethro Hazelhurst
  • 3,230
  • 7
  • 38
  • 80
  • 1
    What you're wanting to do is pass the this context to your getDate function, which you can then wrap the element in jquery using $() and do a .addClass(). See this answer for the exact implementation: http://stackoverflow.com/a/16404397/4987197 – mhodges Oct 04 '16 at 21:09
  • Okay, so I have to explicitly pass 'this', I assume I can still pass my date string as a second argument... going to try that now, thanks for the helpful comment. – Jethro Hazelhurst Oct 04 '16 at 21:12
  • 1
    note that `2016 + /10/ + 4` actually adds a number, to a regular expression's .toString() (which just so happens to be `/10/`). you should probably quote the `/10/`. Oh, and while you're at it, kick the habit of using onclick=. it's never the right tool for the job. – Kevin B Oct 04 '16 at 21:14
  • Yes, you'll have to modify your getDate function to accept the this context as an argument – mhodges Oct 04 '16 at 21:16

1 Answers1

1

First, a DIV tag cannot have an href attribute. Second, what is the need for concatenating your date e.g. 2016 + /10/ + 2? Why not just do 2016/10/2?

Regardless, instead of using onclick attribute, attach an event handler using jQuery's click method. Also, to pass custom information, we'll also use HTML5 data-attribute.

It would look as follows:

HTML

<div class="day" data-date="2016/10/2">2</div>

JavaScript

$('.day').click(function () {
    var dateToday = moment();
    var selectedDate = moment($(this).data('date'));

    // this refers to the DIV, so we need to move one level up to get the related TD

    if (selectedDate.isBefore(dateToday, 'day')) {
        $(this).closest('td').addClass('day-expired');
    } else {
        $(this).closest('td').addClass('day-selected');
    }
});
Mikey
  • 6,728
  • 4
  • 22
  • 45
  • Hi Mikey, really appreciate the answer here... I am concatenating the dates from variables generated by the Calendar Class in codeigniter and think I have gotten confused with the orders of concatenation... thanks for pointing that out! – Jethro Hazelhurst Oct 04 '16 at 21:19
  • Absolutely brilliant, I did not think of using data attributes but I guess they are pretty standard practice at this point and are safe and reliable to use in most browsers. Going to test everything now. Sorry for the sketchy code! – Jethro Hazelhurst Oct 04 '16 at 21:23
  • Oh it's beautiful. Works like a charm and has simplified my code... Thank you so much! – Jethro Hazelhurst Oct 04 '16 at 21:27
  • 1
    No problem. Unless you are trying to support some ancient browser, `data-attribute` is the way to go. They can be very useful. – Mikey Oct 04 '16 at 21:29