2

I'm trying to manipulate the HTML through getElementsByClass() but for some reason it doesn't work. When I use document.getElementByID() it works? Why is this?

<div class='boldStuff'> <p>Welcome to the site. </p> </div>

<script type="text/javascript">
    document.getElementsByClass('boldStuff').innerHTML = 'Fred Flinstone';
</script>
1
  • 2
    Just an aside: it's "Flintstone", not "Flinstone"... Commented Nov 25, 2011 at 20:04

5 Answers 5

6

Try:

document.getElementsByClassName('boldStuff')[0]
Sign up to request clarification or add additional context in comments.

5 Comments

@Michael is there anything else that can get the class being universally supported?
This is true, you have to do some error checking or just use jQuery.
It isn't even available in IE 8.
Also, it returns an array so you have to choose the first element.
@user172071 For good support you need a custom function or use a library. See related search: stackoverflow.com/search?q=getelementsbyclassname
2

getElementsByClassName returns a NodeList. you will have to use

document.getElementsByClassName('boldStuff')[0].innerHTML

refer the docs at mdn

Comments

2

Here's a widely supported solution:

function getElementsByClassName( root, clss ) {

    var result = [], els, i;

    if( arguments.length < 2 || !root || !clss || root.nodeType !== 1 ) {
        return result;
    }

    clss = clss + '';

    if( root.getElementsByClassName ) {
        result = root.getElementsByClassName( clss );
    } else if( root.querySelectorAll ) {
        result = root.querySelectorAll( '.' + clss );
    } else {
        els = root.getElementsByTagName('*');
        clss = " " + clss + " ";
        for( i = 0; i < els.length; ++i ) {
            if( ( " " + els[ i ].className + " " ).indexOf( clss ) !== -1 ) {
                result.push( els[ i ] );
            }
        }
    }
    return result;
}

Then use it like this:

var bold = getElementsByClassName( document, "boldStuff" );

for( var i = 0; i < bold.length; ++i ) {
    bold[ i ].innerHTML = 'Fred Flinstone';
}

The benefit to this is that it uses the native methods wherever possible.

  • It first tries getElementsByClassName because it is generally fastest.

  • Then it tries querySelectorAll, which will bring in support for IE8.

  • Finally, it does a manual filtering of all elements under the provided root.

1 Comment

Very good code snippet. But, document's node type is 9. You could remove condition root.nodeType !== 1.
1

It returns an array so set the first element of the array's innerHTML

document.getElementsByClassName('boldStuff')[0].innerHTML = 'Fred Flinstone';

Comments

0

The correct method to call is getElementsByClassName(), and it will give you an array of elements.

document.getElementsByClassName('boldStuff')

Anyway this is not compatible with older IE version. Check the compatibility here.

4 Comments

I am pretty sure this only works in Firefox, no? It has been awhile since I've used it - is this supported in all browsers now?
It doesn't work on IE up to 8, only since IE9 it seems to work.
@Michael exactly what I said.
@JoseFaeti, yes as I was typing it. You got to the post button first :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.