Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

add attribute in select option

<select id="select">
  <option value="1" data-foo="dogs">this</option>
  <option value="2" data-foo="cats">that</option>
  <option value="3" data-foo="gerbils">other</option>
</select>

// JavaScript using jQuery
$(function(){
    $('select').change(function(){
       var selected = $(this).find('option:selected');
       var extra = selected.data('foo'); 
       ...
    });
});

// Plain old JavaScript
var sel = document.getElementById('select');
var selected = sel.options[sel.selectedIndex];
var extra = selected.getAttribute('data-foo');
Comment

PREVIOUS NEXT
Code Example
Javascript :: ajax upload image 
Javascript :: collapse uncollapse jquery 
Javascript :: node.js express post query string 
Javascript :: Material-ui add alert icon 
Javascript :: typeof 
Javascript :: for in 
Javascript :: protected route in react js 
Javascript :: determine if array has duplicates lodash 
Javascript :: compare two dates in javascript yyyy-mm-dd 
Javascript :: how to get element by id 
Javascript :: filereader check file type 
Javascript :: how to read breakline in html 
Javascript :: creating a 2d array in js 
Javascript :: check if item not in array node js 
Javascript :: convert queryset to json django 
Javascript :: http to https express js 
Javascript :: angular 2 reactive forms radio button by default checked 
Javascript :: javascript get srollwidth 
Javascript :: Uncaught TypeError: Object prototype may only be an Object or null: undefined 
Javascript :: react app js 
Javascript :: how to remove an object from array in react native 
Javascript :: jquery dropdown select 
Javascript :: javascript urlsearchparams to object 
Javascript :: jqueyr element is hide 
Javascript :: read excel file through nodejs 
Javascript :: jest expect not contain 
Javascript :: redirect in vue 
Javascript :: js get html content 
Javascript :: default selected radio button angular material 
Javascript :: How to fetch API data using POST and GET in PHP 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =