Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get class name by using id in javascript

I have a HTML Element, or list item, which has both an id and a class assigned to it. I am writing automated tests, and I need to check the class for this element. In the test, when I call window.document.getElementById('my-element-id'), it does return the correct element, but there is no class property on it. Is there another way to check the class?

like image 445
Josh Sherick Avatar asked Aug 02 '11 15:08

Josh Sherick


2 Answers

The property you're looking for is className.

Here's a working example: http://jsfiddle.net/xxEtj/

HTML

<ul>
    <li id="myId" class="myClass">Item one</li>
</ul>

JS

var li = document.getElementById('myId');
alert(li.className);
like image 81
Jamie Dixon Avatar answered Oct 18 '22 15:10

Jamie Dixon


document.getElementById('my-element-id').className
like image 20
Jeremy Roman Avatar answered Oct 18 '22 13:10

Jeremy Roman