12

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

drs
  • 5,679
  • 4
  • 42
  • 67
foshoeiyyy
  • 471
  • 3
  • 5
  • 18
  • This should help you. http://javascript.about.com/library/bldom08.htm But quick tip -> Use Jquery for that. It's gonna be way easier. – Fredy31 Dec 16 '11 at 18:15
  • 1
    possible duplicate of [How to Get Element By Class in JavaScript?](http://stackoverflow.com/questions/3808808/how-to-get-element-by-class-in-javascript) - I assume you are dealing with JavaScript. – Felix Kling Dec 16 '11 at 18:17

4 Answers4

26

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).

Blender
  • 289,723
  • 53
  • 439
  • 496
1

I suggest you to use JQuery, where you can use directly CSS selectors (like .yourclass) to find all elements of a given class:

$('.yourclass').doWhatYouWant();

If you prefer not to use JQuery, you can use plain Javascript:

document.getElementsByClassName('my-fancy-class')

But be careful with IE8 incompatibility issue. As an alternative (slower but you can build more complex CSS selectors) you can use:

document.querySelector('.cssSelector')

Which will return one element responding to your CSS selector, or

document.querySelectorAll('.cssSelector')

Which will return multiple elements instead.

Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147
0

You can do this using jquery

$('.className').html();

http://api.jquery.com/html/

Robert
  • 3,074
  • 3
  • 24
  • 32
  • Hey this answer helped me to answer the usage of class element in js. May not have been that relevant to the question above but it helped me to figure out horizontal scrolling problem i was having. – DoodleKana Sep 30 '14 at 21:04
-2

If tou use jQuery you can get it as you would in a CSS selector so:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>

then...

$('.classname').each(function() {
    // get html
    $(this).html();
    // set html
    $(this).html('something');
});

Notice you have also convenient way to manage innerHTML.

Andrea Colleoni
  • 5,919
  • 3
  • 30
  • 49
  • This will only give you html for the first element with this class. In reality there could be many so probably you would have to use something like `each()` or similar to process them all. – ivantod Dec 16 '11 at 18:18
  • Still not good even after the edit... check Blender's answer for correct way to do it. – ivantod Dec 16 '11 at 18:25
  • It should work now... Effectively it doesn't add much information than Blender's answer, but thank you. – Andrea Colleoni Dec 16 '11 at 18:31
  • 1
    Inside of the `each` callback you should be using `$(this).html()` instead of `$('.classname').html()`. My previous comments also apply to all other answers here that are not using `each` and calling methods directly on JQuery selectors. – ivantod Dec 16 '11 at 18:37
  • Correcting already written code outside of an editor is often a road fraught with danger! Thanks again ivantod. – Andrea Colleoni Dec 17 '11 at 00:09