Highlighting today
The following code can be placed anywhere in the html file:
<script> // The following code prints the days of the week as a list // and highlights today var d = new Date(); var n = d.getDay(); var weekday=new Array(7); weekday[0]="Sunday"; weekday[1]="Monday"; weekday[2]="Tuesday"; weekday[3]="Wednesday"; weekday[4]="Thursday"; weekday[5]="Friday"; weekday[6]="Saturday"; document.write("<ul>"); for (var i=0; i < weekday.length; i++){ if (weekday[n]===weekday[i]){ document.write("<li class=\"highlight\">" + weekday[i] + "</li>"); }else{ document.write("<li>" + weekday[i] + "</li>"); } } document.write("</ul>"); </script>
This script prints the days of the week as a list. It does this by stepping through the weekday array, one day at a time using a for
loop. With each pass of the loop, the script uses an if
statement to check to see if the current day name (weekday[i]
) is the same as today (weekday[n]
). If it is, it writes an opening list item tag with a class and if it isn't it writes a list item without a class. The class is styled in the usual way, using CSS.
.highlight { color:#C00; }