admin管理员组文章数量:1302418
If next mand:
console.log(document.getElementById('container'));
prints:
<div id="container" prjid="ABCDE">...</div>
why the next mand:
console.log(document.getElementById('container').prjid);
prints undefined? I am trying to get the value of prjid
If next mand:
console.log(document.getElementById('container'));
prints:
<div id="container" prjid="ABCDE">...</div>
why the next mand:
console.log(document.getElementById('container').prjid);
prints undefined? I am trying to get the value of prjid
Share Improve this question edited Feb 3, 2018 at 9:39 Ulysse BN 11.4k7 gold badges62 silver badges93 bronze badges asked Feb 3, 2018 at 8:55 Jose Cabrera ZunigaJose Cabrera Zuniga 2,6376 gold badges39 silver badges67 bronze badges 4-
1
You shouldn't use nonstandard attributes. If you need to add your own data to an element, use
data-prjid
. – Barmar Commented Feb 3, 2018 at 9:00 - @ Jose Cabreta Zuniga, did it work ? – Sudhakar Commented Feb 3, 2018 at 9:07
- Possible duplicate of How to get value of a div using javascript – Ulysse BN Commented Feb 3, 2018 at 9:17
- all is working now. Why I should not use nonstandard attributes? – Jose Cabrera Zuniga Commented Feb 5, 2018 at 0:30
6 Answers
Reset to default 6prjid
is an attribute. You should use the function getAttribute
to get any attributes value.
getAttribute() returns the value of a specified attribute on the element. If the given attribute does not exist, the value returned will either be null or "" (the empty string);
console.log(document.getElementById('container').getAttribute("prjid"));
<div id="container" prjid="ABCDE">...</div>
In order to get prjid
which isn't a defined attribute on div rather a custom one, you would use getAttribute
document.getElementById('container').getAttribute('prjid')
Snippet
console.log(document.getElementById('container').getAttribute('prjid'));
<div id="container" prjid="abd"/>
According the MDN docs:
getAttribute() returns the value of a specified attribute on the element. If the given attribute does not exist, the value returned will either be null or "" (the empty string);
Note: In React you shouldn't use document.getElementById and rather use refs. Check this answer
In order to get prjid use getAttribute
document.getElementById('container').getAttribute('prjid');
getAttribute()
returns the value of a specified attribute on the element. If the given attribute does not exist, the value returned will either be null or "" (the empty string);
Instead of doing this you can get data attribute to that like below
document.getElementsById("container").getAttribute("prjid");
You can get it by getAttribute
function like
console.log(document.getElementById('container').getAttribute("prjid"));
You can read about this here
if you want get an value , and that value place in an custom attribute you must use getAttribute() method , some thing like this
var pjid = document.getElementById('container').getAttribute('pjid');
and create this attribute in your element
<div id="container" pjid="some-thing" >
but i thing you are this problem in React , because you Tag reactjs , in react ( prev version - less than 16 ) , JSX delete all undefined attribute but this problem solve in react 16 , you must migrate to this version
本文标签: javascriptGetting the value of a div tag39s special attributeStack Overflow
版权声明:本文标题:javascript - Getting the value of a div tag's special attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741685485a2392419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论