Answers for "add selected attribute to option javascript"

0

how to add attribute to selected element in javascript

// Selecting the element
    var btn = document.getElementById("myBtn");
	
    // Setting new attributes
    btn.setAttribute("class", "click-btn");
    btn.setAttribute("disabled", "");
Posted by: Guest on June-03-2020
1

make select option selected javascript

document.getElementById('personlist').getElementsByTagName('option')[11].selected = 'selected'
Posted by: Guest on October-07-2020
0

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');
Posted by: Guest on March-15-2021

Code answers related to "add selected attribute to option javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language