Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  156] [ 1]  / answers: 1 / hits: 72436  / 13 Years ago, fri, december 16, 2011, 12:00:00

I want to know if there is a way to getElementByClassName(classname).innerHTML function or something to the equivalent of getElementById(ClassName).innerHTML.


More From » html

 Answers
24

You are missing an s in your function name. getElementsByTagName returns a collection of elements, of elements, which you need to iterate over:



var elements = document.getElementsByClassName(classname);

for (var i = 0; i < elements.length; i++) {
elements[i].innerHTML = 'foo';
}


IE8 and below don't support getElementsByClassName, so you'll have to find a polyfill or use querySelectorAll (IE8).


[#88520] Thursday, December 15, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tomas

Total Points: 165
Total Questions: 111
Total Answers: 103

Location: Maldives
Member since Tue, Dec 21, 2021
2 Years ago
tomas questions
Thu, Jan 27, 22, 00:00, 2 Years ago
Mon, May 10, 21, 00:00, 3 Years ago
Tue, Jan 5, 21, 00:00, 3 Years ago
;