admin管理员组文章数量:1022743
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);
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);
Share
Improve this question
edited Sep 13, 2012 at 4:32
Felix Kling
818k181 gold badges1.1k silver badges1.2k bronze badges
asked Sep 13, 2012 at 4:17
DanielDaniel
4,36212 gold badges52 silver badges69 bronze badges
1
- possible duplicate of DOM attribute access: why is "elt.class" not working? – Felix Kling Commented Sep 13, 2012 at 4:34
2 Answers
Reset to default 3Instead 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
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);
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);
Share
Improve this question
edited Sep 13, 2012 at 4:32
Felix Kling
818k181 gold badges1.1k silver badges1.2k bronze badges
asked Sep 13, 2012 at 4:17
DanielDaniel
4,36212 gold badges52 silver badges69 bronze badges
1
- possible duplicate of DOM attribute access: why is "elt.class" not working? – Felix Kling Commented Sep 13, 2012 at 4:34
2 Answers
Reset to default 3Instead 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
本文标签: javascriptget element by id and then class attributeStack Overflow
版权声明:本文标题:javascript - get element by id and then class attribute - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745500329a2153361.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论