I am trying to create some short code that will change the class of a group of links based on the current month. Basically, if the month has past, "a" will be class="inactive", if the month is current, "a" will be class="active", and if the month hasn't occurred yet it will be class="none".
This is for some blog code that I want to automatically update with javascript so that when the month changes, the recently past month (and all previous months) switch to the class "inactive" and the new month becomes "active", which are styles defined by my CSS.
Here is my HTML for the block:
<div class="container">
<div><a id="item1" class="inactive" href="january.html">january</a></div>
<div><a id="item2" class="inactive" href="february.html">february</a></div>
<div><a id="item3" class="inactive" href="march.html">march</a></div>
<div><a id="item4" class="inactive" href="april.html">april</a></div>
<div><a id="item5" class="inactive" href="may.html">may</a></div>
<div><a id="item6" class="inactive" href="june.html">june</a></div>
<div><a id="item7" class="inactive" href="july.html">july</a></div>
<div><a id="item8" class="active" href="august.html">august</a></div>
<div><a id="item9" class="none" href="september.html">september</a></div>
<div><a id="item10" class="none" href="october.html">october</a></div>
<div><a id="item11" class="none" href="november.html">november</a></div>
<div><a id="item12" class="none" href="december.html">december</a></div>
</div>
As you can see the idea is to use a getMonth() script to change the class of the August code to "inactive", and change the September class to "active" once we reach the month of September.
I have seen a lot of snippets of Javascript that could work but am honestly not sure of the best route to take to accomplish this, as my Javascript skills are not so great. Perhaps a loop would be preferable since it can take an ID of the "a", maybe using an array, that could run a check then write the appropriate class based on a getMonth() variable?
Here is some of the code I'm working with so far:
<script>
var item = [1,2,3,4,5,6,7,8,9,10,11,12];
iLen = item.length;
for (i = 0; i < iLen; i++) {
if(item[i]<getMonth()) {
document.getElementById("item").class="inactive";
} else if(item[i]==getMonth()) {
document.getElementById("item").class="active";
} else if(item[i]>getMonth()) {
document.getElementById("item").class="none";
}
</script>
Unfortunately I have a hard time with array's so I am running into issues with replacing the class code itself, but I would love your feedback about if this is a good way to go about it.