简体   繁体   中英

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);

Instead of simply class , use className :

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

You may use: getAttribute("class")

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

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM