0

I am trying to get the body's classname to use in an if/else statement in plain javascript.

To my surprise the element.className trows me errors everytime:

Uncaught TypeError: Cannot read property 'className' of undefined

 alert( document.getElementsByTagName("body")[0].className.match("home") );
 alert( document.getElementById("container").className.match("fooclass") );
<body class="home page">
 <div id="container" class="fooclass"></div>
</body>
FFish
  • 10,964
  • 34
  • 95
  • 136

1 Answers1

0

Try this

window.onload = function(){

    alert( document.getElementsByTagName("body")[0].className.match("home") );
    alert( document.getElementById("container").className.match("fooclass") );

}

You can put this anywhere on the page ;)

gurvinder372
  • 66,980
  • 10
  • 72
  • 94