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
  • 11,014
  • 33
  • 93
  • 135

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
  • 64,240
  • 8
  • 67
  • 88