31

Want to get data attribute value from selected dropdown option.

<select name="selection" id="mySelect">
    <option value="21" data-rc="25" data-clnc="10">Treatment</option>
</select>

var rc = ? //value of data-rc
var clnc = ? //value of data-clnc

No jQuery please only JavaScript :)

1

7 Answers 7

48

You can read them out via dataset property.

var option = document.getElementsByTagName("option")[0];

console.log(option.dataset.rc)
console.log(option.dataset.clnc)
<option value="21" data-rc="25" data-clnc="10">Treatment</option>

Or, if you want to get the values of the selected option:

var selection = document.getElementById("mySelect");

selection.onchange = function(event){
  var rc = event.target.options[event.target.selectedIndex].dataset.rc;
  var clnc = event.target.options[event.target.selectedIndex].dataset.clnc;
  console.log("rc: " + rc);
  console.log("clnc: " + clnc);
};
<select name="selection" id="mySelect">
<option value="21" data-rc="25" data-clnc="10">Treatment</option>
<option value="21" data-rc="23" data-clnc="30">Treatment1</option>
<option value="21" data-rc="31" data-clnc="50">Treatment2</option>
<option value="21" data-rc="14" data-clnc="75">Treatment3</option>
</select>

Sign up to request clarification or add additional context in comments.

4 Comments

Index is not always 0 :(
If you want to get the values of the selected option, I edited the answer. If you want to get all the values, you'll have to iterate. Anyway, now you get the general idea.
Thanks, I forget the options property of the target. 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.
Your answer gave me a great idea how to fix a similar problem I was having :D cheers
23

suppose we have a select field

    <select id="ddlViewBy">
      <option value="1" data-rc="25" data-clnc="10" selected="selected">test1</option>
      <option value="2" >test2</option>
      <option value="3">test3</option>
    </select>

Now we will get the select list and its selected option

    var e = document.getElementById("ddlViewBy");
    var option= e.options[e.selectedIndex];

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");

1 Comment

Good. This is the alternative way for working with data attributes. The other one is using dataset, which I prefer, although may not work in some browsers.
6

Check this working pen

working pen

$('#options').on('change', function(){
    alert($(this).find("option:selected").attr('data-rc'))
    alert($(this).find("option:selected").attr('data-clnc'))
});

1 Comment

No jQuery please only JavaScript
3

    var mySelect = document.querySelector('#mySelect')
        console.log('mySelect value ' + mySelect.value)
        console.log('mySelect data-rc ' + mySelect.selectedOptions[0].getAttribute("data-rc"))
        console.log('mySelect data-clnc ' + mySelect.selectedOptions[0].getAttribute("data-clnc"))
<select name="selection" id="mySelect">
    <option value="21" data-rc="25" data-clnc="10">Treatment</option>
</select>

Comments

1

$(selector).find("option:selected").data("rc") for rc and clnc for clnc where selector is your "select" tag id/class

2 Comments

No jQuery please only JavaScript
No jQuery. Instead, use DOM API, from which we can use either dataset property or getAttribute method of the corresponding DOM element.
1

If anybody looking for the simplest answer, here is the solution.

Javascript:

function getURL()
{
    var link = $("#title :selected").data('link');
    console.log(link);
}

HTML:

<select id="title" onchange="getURL()">
    <option data-link="https://www.google.com" value="1">Single</option>
    <option data-link="https://www.google.com/search" value="2">Double</option>
</select>

Happy Programming!!!

Comments

0

You can do that with jquery selector

var rc = $('select option:selected').data('rc');

1 Comment

No, it's said above that no jQuery. There are 2 correct answers that other guys had given : one using dataset, the other using getAttribute. All use plain DOM API, no jQuery involved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.