Daniel
Daniel

Reputation: 4342

get element by id and then class attribute

I am trying to get an elements class name. First I find the element by its id and then I tried to get the class attribute doing the following. My results return undefined. How can I get the text from the class attribute? Which would be "not-checked-in".

html

<div id="last-check-in" class="not-checked-in"></div>

javascript

var checkedin;
checkedin = document.getElementById("last-check-in");
console.log(checkedin.class);

Upvotes: 1

Views: 6459

Answers (2)

Ahsan Khurshid
Ahsan Khurshid

Reputation: 9469

You may use: getAttribute("class")

var checkedin;
checkedin = document.getElementById("last-check-in");
console.log(checkedin.getAttribute("class"));

DEMO

Upvotes: 1

Joseph Silber
Joseph Silber

Reputation: 219946

Instead of simply class, use className:

var checkedin = document.getElementById("last-check-in");
console.log(checkedin.className);

Upvotes: 3

Related Questions