Javascript – How to get HTML elements by class in JavaScript

javascript

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>

Best Answer

Try:

document.getElementsByClassName('boldStuff')[0]