Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PURE JS get selected option data attribute value returns Null

Tags:

javascript

I have this html:

<select onchange="check_status(this);" name="status[171]">         <option selected="true" value="open" data="04f2cf35e4d7a1c0158459fd0450a605">open</option>         <option value="in_process" data="04f2cf35e4d7a1c0158459fd0450a605">pending</option>         <option value="finished" data="04f2cf35e4d7a1c0158459fd0450a605">finished</option>         <option value="canceled" data="04f2cf35e4d7a1c0158459fd0450a605">canceled</option>     </select>     

and js

function check_status(obj){         var uid = obj.getAttribute('data');     alert(uid); } 

but it always alerts null instead of data value Where is the problem guys? Thanks

like image 244
Europeuser Avatar asked Jan 22 '15 09:01

Europeuser


People also ask

How to get attribtues from a selected option in JavaScript?

Now we have the selected option we can get its attribtues var attrs = option.attributes; attrs is the attributes array you can get attribtues by index you want. Or you can get attribtues by var datarc = option.getAttribute("data-rc");

Is it possible to get the attribute of an element using jQuery?

No jQuery. Instead, use DOM API, from which we can use either dataset property or getAttribute method of the corresponding DOM element. – Lex Soft Mar 7 '19 at 6:13

How to get all the values of the selected option?

If you want to get the values of the selected option, I edited the answer. If you want to get allthe values, you'll have to iterate. Anyway, now you get the general idea.

Do you prefer getatttibute or dataset for HTML elements/nodes?

Nowadays I tend to be reluctant to dig deeper into HTML elements/nodes with their properties/methods. I prefer using dataset, instead of getAtttibute, although dataset may not work in some browsers. – Lex Soft Mar 7 '19 at 6:05


2 Answers

The problem is that you get select element and not selected option element as function argument. And it does not have the data attribute. You have to get the option attribute like so:

function check_status(obj) {   var uid = obj.options[obj.selectedIndex].getAttribute('data-uid');   alert(uid); }
<select onchange="check_status(this);" name="status[171]">    <option selected="true" value="open" data-uid="01f2cf35e4d7a1c0158459fd0450a601">open</option>   <option value="in_process" data-uid="02f2cf35e4d7a1c0158459fd0450a602">pending</option>   <option value="finished" data-uid="03f2cf35e4d7a1c0158459fd0450a603">finished</option>   <option value="canceled" data-uid="04f2cf35e4d7a1c0158459fd0450a604">canceled</option> </select>
Notice that I changed the attribute name to data-uid for it to be valid according to HTML5 specificaion.
like image 95
Jakub Miziołek Avatar answered Sep 23 '22 03:09

Jakub Miziołek


You are trying to get select data attribute, and not option's.

Also, I can see that all you data attributes are identical. Then you can move it from option to select itself: <select onchange="check_status(this);" name="status[171]" data="04f2cf35e4d7a1c0158459fd0450a605" > and use code snipped from your question unmodified.

function check_status(obj) {    var uid =  obj.options[obj.selectedIndex].getAttribute('data');    alert(uid)  }
<select onchange="check_status(this);" name="status[171]">    <option selected="true" value="open" data="open04f2cf35e4d7a1c0158459fd0450a605">open</option>    <option value="in_process" data="pending104f2cf35e4d7a1c0158459fd0450a605">pending</option>    <option value="finished" data="finished04f2cf35e4d7a1c0158459fd0450a605">finished</option>    <option value="canceled" data="canceled04f2cf35e4d7a1c0158459fd0450a605">canceled</option>  </select>
like image 32
glyuck Avatar answered Sep 24 '22 03:09

glyuck