2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

選択されているOptionに設定されているカスタム属性の値を取る方法

Posted at

datesetで指定したら取れるかと思ったが、カスタム属性の値を取るには少し工夫が必要だった。

<select id="pet" name="pet">
    <option value="tamajirou" data-age="2" selected>たまじろう</option>
    <option value="tamao" data-age="3">たまお</option>
    <option value="tamako" data-age="5">たまこ</option>
</select>       
//対象のSelect
const pet = document.getElementById('pet');

//選択されているoptionを抽出
const selectedOption = Object.values(this.staff.options).find(option => option.selected === true);

//たまじろうのageが出力される
console.log(select.dataset.age)
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?