最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Get data-* attribute of the selected <option> - Stack Overflow

matteradmin6PV0评论

I'm trying to get the second value of an option tag using document.get.elementbyId.

<select id="test" class="form-control">
  <option value="">-- Select --</option>
  <option value="1" data-doj="20-06-2011">John</option>
  <option value="2" data-doj="10-05-2015">Clif</option>
  <option value="3" data-doj="01-01-2008">Alexander</option>
</select>

I'm trying to get the second value of an option tag using document.get.elementbyId.

<select id="test" class="form-control">
  <option value="">-- Select --</option>
  <option value="1" data-doj="20-06-2011">John</option>
  <option value="2" data-doj="10-05-2015">Clif</option>
  <option value="3" data-doj="01-01-2008">Alexander</option>
</select>

Usually, I would use document.getElementById("test").value; to get the value of one option. What should I do if I have multiple values like in this case? document.getElementById("test").data-doj;? Thank you.

Share Improve this question edited Apr 16, 2020 at 10:05 Yevhen Horbunkov 15.6k3 gold badges27 silver badges45 bronze badges asked Apr 16, 2020 at 9:26 Flick nkFlick nk 311 silver badge7 bronze badges 1
  • there's an API to access data-* attributes, you may check out the example for your use case over here – Yevhen Horbunkov Commented Apr 16, 2020 at 10:09
Add a ment  | 

4 Answers 4

Reset to default 2

Use HTMLSelectElement.selectedIndex

The HTMLSelectElement.selectedIndex is a long that reflects the index of the first or last selected element, depending on the value of multiple. The value -1 indicates that no element is selected.

To access data-* attributes, use dataset

Note - this in event-handler refers to the element on an event is invoked.

let select = document.getElementById("test");
select.onchange = function() {
  let selectedI = this.selectedIndex;
  console.log(this.options[selectedI].dataset.doj)
};
<select id="test" class="form-control">
  <option value="">-- Select --</option>
  <option value="1" data-doj="20-06-2011">John</option>
  <option value="2" data-doj="10-05-2015">Clif</option>
  <option value="3" data-doj="01-01-2008">Alexander</option>
</select>

Like this

navigate using selectedIndex

document.getElementById("test").addEventListener("change",function() {
  const opt = this.options[this.selectedIndex];
  console.log(opt.value, 
    opt.getAttribute("data-doj"),  // or opt.dataset.doj
    opt.text)
})
<select id="test" class="form-control">
  <option value="">-- Select --</option>
  <option value="1" data-doj="20-06-2011">John</option>
  <option value="2" data-doj="10-05-2015">Clif</option>
  <option value="3" data-doj="01-01-2008">Alexander</option>
</select>

Strangely, no one still suggested.

There's a way to access HTMLCollection of the selected options with HTMLSelecteElement.selectedOptions. If you have only one <option> selected at a time, you may simply pull its first element (with [0]).

To access data-* attribute there's a proper API, which implies .dataset['propertyname'] kind of syntax:

document.getElementById('test').addEventListener('change', function(){
  const [selectedOption] = this.selectedOptions,
        dataDoj = selectedOption.dataset.doj
  console.log(dataDoj)        
})
<select id="test" class="form-control">
<option value="">-- Select --</option>
<option value="1"  data-doj="20-06-2011">John</option>
<option value="2"  data-doj="10-05-2015">Clif</option>
<option value="3"  data-doj="01-01-2008">Alexander</option>
</select>

Please see the working example below, How you can get value, text and custom attribute of selected option -

function trackValue(){ 
var element = document.getElementById("test");
var option_value = element.options[element.selectedIndex].value;
var option_text = element.options[element.selectedIndex].text;
var option_doj = element.options[element.selectedIndex].getAttribute('data-doj')
console.log('value-', option_value);
console.log('text-', option_text);
console.log('doj-', option_doj);
}
<select id="test" class="form-control" onChange="trackValue();">
<option value="">-- Select --</option>
<option value="1"  data-doj="20-06-2011">John</option>
<option value="2"  data-doj="10-05-2015">Clif</option>
<option value="3"  data-doj="01-01-2008">Alexander</option>
        </select>

Post a comment

comment list (0)

  1. No comments so far