I have 5 <p class ='date'></p>
elements to all of which I want to append a current date, how can I accomplish this using JavaScript.
Asked
Active
Viewed 38 times
0

Imo
- 1,455
- 4
- 28
- 53
2 Answers
0
var newDate = new Date().toISOString(); // Or whatever date you need
var elements = document.getElementsByClassName("date");
for(var i = 0; i < elements.length; i++) {
elements[i].textContent += newDate;
}

Perostek Balveda
- 95
- 5
-1
you said javascript not jquery ! so this way :
<p class ='date'></p>
<p class ='date'></p>
<p class ='date'></p>
<p class ='date'></p>
<p class ='date'></p>
<p>i don't have the class "date"</p>
<script>
var today="2015/x/x";//assume you have the date!
var paras=document.getElementsByClassName("date");
for(i=0;i<paras.length;i++)
{
paras[i].textContent+=today;
}
</script>
*** i corrected my answer !

NO-ONE_LEAVES_HERE
- 127
- 1
- 11
`s. And then add the formatted date like suggested here: http://stackoverflow.com/questions/5677799/how-to-append-data-to-div-using-javascript
– MBaas Oct 08 '15 at 12:37