0

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.

Imo
  • 1,455
  • 4
  • 28
  • 53
  • 1
    What - using the same id 5 times??? id should be unique, try to find a common class that you only assign to these `

    `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
  • ID's should always be unique... And if you want to Change them to the current date, take the class Attribute – Luca Oct 08 '15 at 12:44
  • Thank you for noticing that corrected it, I just want to learn a the way to append elements to all divs having same class – Imo Oct 08 '15 at 13:01

2 Answers2

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;
}
-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 !