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
Add a ment  | 

6 Answers 6

Reset to default 6

prjid 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