0

What I want to achieve is populating many form fields with the same value. I can do it for an element that has an id:

function selectDateAndArendeSearch(year, month, day, field, number, numberfield) {
    var m = parseInt(month)+1;

    if(m<10)
        month="0"+m;
    else
        month=m;
    if(day.length==1)
        day="0"+day;

    document.actionForm.all(field).value = year+"-"+month+"-"+day;
    document.actionForm.all(numberfield).value = number;
    document.getElementById('popupF').style.display = 'none';
}

But how do I do it for many elements i.e. a class?

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424

3 Answers3

2
for (el in document.getElementsByClassName('myclassname')) {
    el.value = "New Value";
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
1
document.getElementsByClassName('myclassname').value = 'New Value';
Raab
  • 34,778
  • 4
  • 50
  • 65
  • 1
    There is no such function, it's `getElementsByClassName` and it returns an array, which you have to iterate over. – Barmar Oct 25 '12 at 06:38
1

The method getElementsByClassName is perfect for the mayority of modern browsers.
However if you need compatibility with old browsers, it might not be native. In this case, have a look to CMS's answer to this question, who provides a portable function for this task.

For compatibility see the following working draft

Community
  • 1
  • 1
Luca Borrione
  • 16,324
  • 8
  • 52
  • 66