I'm trying to display parts of the date on a page using the ID, I have to use the ID multiple times on a page, and I've been told that I need to change that to class, and I'm unsure how to do that.
I've created this jsfiddle.
The HTML is as follows, but I need to use some of those ID's multiple time, and using a second instance of it doesn't show/work.
<p>The date is <a id="date"></a></p>
<p>The current month and year is <a id="month-year"></a></p>
<p>The current day and month is <a id="day-month"></a></p>
<p>The current year is <a id="year"></a></p>
<p>The current month is <a id="month"></a></p>
<p>Today is the <a id="day"></a> of <a id="month"></a></p>
In the last example <a id="month">
will not show.
the JS I'm using is as follows.
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var n = new Date();
var y = n.getFullYear();
var m = n.getMonth();
var d = n.getDate();
var dateObject = document.getElementById("date");
if (dateObject) dateObject.innerHTML = d + " " + months[m] + " " + y;
var month_year = document.getElementById("month-year");
if (month_year) month_year.innerHTML = months[m] + " " + y;
var day_month = document.getElementById("day-month");
if (day_month) day_month.innerHTML = d + " " + months[m];
var year = document.getElementById("year");
if (year) year.innerHTML = y;
var month = document.getElementById("month");
if (month) month.innerHTML = months[m];
var day = document.getElementById("day");
if (day) day.innerHTML = d;
How can I change the above code so I can use the ID/class multiple times on a page?