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,538
  • 30
  • 89
  • 133

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
  • 61,170
  • 7
  • 61
  • 75