Advertisement
trentjs

Read Select Option Data Attribute

Jan 15th, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.  
  5. <style>
  6.  
  7.     body{
  8.         background-color: #FFFFFF;
  9.         font-family: Arial, Helvetica, sans-serif;
  10.     }
  11.  
  12. </style>
  13.  
  14. </head>
  15. <body>
  16.  
  17. <h2>HTML Content</h2>
  18.  
  19. <select name="selection" id="select00">
  20.     <option value="Default" data-info="Info 00">Select Option</option>
  21.     <option value="Value 01" data-info="Info 01">Option 01</option>
  22.     <option value="Value 02" data-info="Info 02">Option 02</option>
  23.     <option value="Value 03" data-info="Info 03">Option 03</option>
  24.     <option value="Value 04" data-info="Info 04">Option 04</option>
  25. </select>
  26.  
  27. <script>
  28.  
  29. var selectSrc = document.getElementById("select00");
  30. selectSrc.addEventListener("change", getSelectData);
  31.  
  32. function getSelectData(event){
  33.     var optionIndex = event.target.selectedIndex;
  34.     var optionValue = event.target.value;
  35.     var dataSrc = event.target.options[event.target.selectedIndex].dataset.info;
  36.     console.log("optionIndex:" + optionIndex + " optionValue:" + optionValue  + " dataSrc: " + dataSrc );
  37. }
  38.  
  39. </script>
  40.  
  41. </body>
  42. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement