Answers for "select option js"

4

jquery add option to select

//add option to select with jQuery
$('#selectID').append($('<option>', {
    value: 1,
    text: 'Option Text'
}));
Posted by: Guest on July-31-2019
0

add option to select jquery

$.each(items, function (i, item) {
    $('#mySelect').append($('<option>', { 
        value: item.value,
        text : item.text 
    }));
});
Posted by: Guest on March-18-2020
1

make select option selected javascript

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

options in html

<form id="form">
            <label for=""></label>
            <br>
            <input type="" id="">
            <br>
            <label for=""></label>
            <br>
            <input list="browsers" name="Thenicknamey" id="Thenickname">
            <datalist id="browsers">
                <option value=""></option>
                <option value=""></option>
                <option value=""></option>
            </datalist>
            <br>
            <label for=""></label>
            <br>
            <input type="" id="">
            <br>
            <input type="" id="">
        </form>
Posted by: Guest on October-10-2020
0

js select option in form

<form name="AddAndEdit">
   <select name="list" id="personlist">
      <option value="P1">Person1</option>
      <option value="P2">Person2</option>
      <option value="P3">Person3</option>
   </select>
</form>
Posted by: Guest on July-10-2020
0

javascript option

/* assuming we have the following HTML
<select id="s">
    <option>First</option>
    <option>Second</option>
    <option>Third</option>
</select>
*/ 

var s = document.getElementById('s');
var options = [ 'zero', 'one', 'two' ];

options.forEach(function(element, key) {
  if (element == 'zero') {
    s[s.options.length] = new Option(element, s.options.length, false, false);
  }                
  if (element == 'one') {
    s[s.options.length] = new Option(element, s.options.length, true, false); // Will add the "selected" attribute    
  }
  if (element == 'two') {
    s[s.options.length] = new Option(element, s.options.length, false, true); // Just will be selected in "view"
  }
});

/* Result
<select id="s">
  <option value="0">zero</option>
  <option value="1" selected="">one</option>
  <option value="2">two</option> // User will see this as 'selected'
</select>
*/
Posted by: Guest on April-14-2020

Browse Popular Code Answers by Language