Highlighting Todays Work Day of the Week in an HTML Table using only CSS/Javascript/Jquery

Advertisements

I would like my computer to automatically highlight todays day of the week in a table row.

The table has all the weekdays in a separate row, and has a fixed number of seven rows, starting with the least pleasant day of all days, Monday or Maandag in Dutch.

Lets say its Tuesday today or Dinsdag in Dutch, then the second row should be highlighted like so:

However, the problem is that instead of todays day, the next day is highlighted.
Probably I’ve overlooked something small.
The solution can be onlly in HTML/CSS/JS/JQuery and not in PHP.
I have made a demo of my half-working half-defective exemaple:

https://jsfiddle.net/u0bvtzd1/3/

JS

workday();

function workday() {
  var d = new Date().getDay();
  document.getElementById("day-"+d).classList.add('today');    
}

HTML

<h2>Werktijden<h2>  

<table>
    <tbody>
        <tr id="day-0">
            <td>Maandag</td>
        </tr>
        <tr id="day-1">
            <td>Dinsdag</td>
        </tr>
        <tr id="day-2">
            <td>Woensdag</td>
        </tr>
        <tr id="day-3">
            <td>Donderdag</td>
        </tr>
        <tr id="day-4">
            <td>Vrijdag</td>
        </tr>
        <tr id="day-5">
            <td>Zaterdag</td>
        </tr>
        <tr id="day-6">
            <td>Zondag</td>
        </tr>
    </tbody>
</table>

>Solution :

Happens because Monday is the first day of the week not 0th.

Start numbering ids from 1 not 0

Leave a ReplyCancel reply