javascript append option to select and set selected code example

Example 1: To append dropdown option using jquery

$('#ddlGroup').append(new Option('Select group', '0'));

Example 2: how to append the dropdown values by jquery each function

BY LOVE

 $.each(obj , function (key, value) {
                                $('#GroupName').append($('<option>',
                                    {
                                    value: value.id,
                                    text: value.Group_Name
                                }));

Example 3: javascript add option element to select

javascript add option/element to select:
const newOpt = document.getElementById('option-input');
let mySelect = document.getElementById('mySelect'); //Object not array
const opts = Object.values(mySelect.options);
const option = document.createElement('option');
optValues = opts.map(opt => opt.value.toLowerCase());
//Validate if option exists
if(optValues.indexOf(newOpt.value.toLowerCase()) !== -1){
  	newOpt.nextElementSibling.after("Category exists");
   }else{
   option.value = newOpt.value;
    option.text = newOpt.value;
    mySelect.add(option);
    mySelect.value = option.value;
   }

Example 4: add select option jquery

Add option to a select list of picklist

Tags:

Html Example