How to get value of a custom attibute inside a <option> tag using JAVASCRIPT

MaDjorz

My dropdown has a custom attributes and what I want to happen is to display the custom attributes value in a label tag.

Here is a code snippet:

function setDesignation(d){
  var designation = d.getAttribute("data-designation");
  if(designation != null){
  	document.getElementById('designation').innerHTML = designation;
  }else{
  	document.getElementById('designation').innerHTML = 'Name';
  }
  //alert(d.getAttribute("data-designation"));
}
<div>
    <select onchange="setDesignation(this)">
        <option value="1" data-designation="President">Mathew</option>
        <option value="2" data-designation="CEO">Mark</option>
        <option value="3" data-designation="Manager">Luke</option>\
        <option value="4" data-designation="">John</option>
    </select>
</div>

<div>
    <label id="designation">Designation</label>
</div>

The problem is that I can't get the value of the custom attributes which is the "data-designation". I tried to put it in an alert() method to see the value and it says "null". Even I remove the if else statement it still returns null. I don't know what wrong with the code so please help me.

I tried to find solution but all that I found are jQuery. I'm not good in jQuery so I would like a PURE JAVASCIPT solution.

P.S. if your solution is to store it in the value attribute then thats not what I'm looking for.

Jonas Wilms

You dont want the selects data attribute, you want the selected options data attribute:

var designation = d.options[d.selectedIndex].getAttribute("data-designation");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to retrieve the text of option tag using the value given to it in the option tag using javascript?

How to get the value of a body tag using javascript?

how to get literal array value stored inside script tag on html page using javascript or protractor

How to get the complete value of an option tag?

How to get the option value in javascript

How can i get the value of the selected option using Javascript or Jquery?

How to get value of <a> tag in JavaScript

How do I get the value of input tag using javascript

How to get the value of a specific tag inside XML

How to get attribute value inside li tag?

How to extract src attibute of a HTML tag?

How to get only text inside div tag and src content of img tag in by using regular expression javascript

GTM - get href value inside custom html tag

Using value of JavaScript inside video HTML tag inside of .setContent() in Leaflet

how can I get a value inside Tr using javascript

Get select option by value using javascript

How to get input tag and option tag separately and load inside div and inside select

Using javascript to change <a> tag to use selected option value

Get content of tag value using Javascript

How to insert <img> tag inside <option> tag?

How to get text inside specific tag using tag name with Python

How to get the value for an option using ArgResults in dart

how to get value from select tag in javascript

How to get the element value of certain tag in javascript?

How to add value into option using javascript

How to pass an object to the value property in option tag using angular?

Get custom attribute value from multiple select option using Jquery

Add custom option (text + value) in a select tag

get all the text inside HTML span tag and clustering it using javascript