Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
108
rated 0 times [  111] [ 3]  / answers: 1 / hits: 59971  / 11 Years ago, thu, october 10, 2013, 12:00:00

I have several textboxes with the class output. I would like to be able to print their values as a plain HTML list in a div with ID combined. Right now, I have the following code:



function doCombine() {
document.getElementById('combined').innerHTML =
document.getElementsByClassName('output').value + ,;
}


Yet, when I run the function, i get the error message undefined,. When i add a [0] before .value, it works, but only the value of the first textbox is showing up. I read somewhere that [i] will show all the values, but that doesn't seem to work.



What am I doing wrong?


More From » javascript

 Answers
11

getElementsByClassName



Returns a set of elements which have all the given class names. When called on the document object, the complete document is searched, including the root node. You may also call getElementsByClassName on any element; it will return only elements which are descendants of the specified root element with the given class names.



So you should be doing



var elements = document.getElementsByClassName('output');
var combined = document.getElementById('combined');
for(var i=0; i < elements.length; i++) {
combined.innerHTML += elements[i].value + ,;
}

[#75104] Wednesday, October 9, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darrell

Total Points: 109
Total Questions: 113
Total Answers: 113

Location: Zambia
Member since Sat, Oct 31, 2020
4 Years ago
;