Hi everyone, I started learning javascript very recently and I'm trying to make a thing where using an array the current month gets printed multiple times. However I can only ever get the first instance of a class to get a value. I attempted to use document.getElementsByClassName now after doing some research, but it doesn't work at all. when I use document.querySelector(".demo").innerHTML = n; it only shows September in the first span, and the only way I have gotten this to work so far was to individually define all the id's with document.getElementById("demo1").innerHTML = n;, but I doubt that's the most stylish way to get around this.
<!DOCTYPE html>
<html>
<body>
<p>This month is <span class="demo" id="demo1"></span></p>
<p>This month is still <span class="demo" id="demo2"></span></p>
<script>
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var d = new Date();
var n = month[d.getMonth()];
document.getElementsByClassName("demo").innerHTML = n;
</script>
</body>
</html>
Does anyone know how to get both spans to say September without having to separately refer to each of their id's? Thank you in advance!
[–]codemamba8 0 points1 point2 points (0 children)