1) Using the Option constructor and add() method
2) Using the DOM methods
let newOption = new Option('Option Text','Option Value'); const select = document.querySelector('select'); select.add(newOption,undefined);
// create option using DOM const newOption = document.createElement('option'); const optionText = document.createTextNode('Option Text'); // set option text newOption.appendChild(optionText); // and option value newOption.setAttribute('value','Option Value'); const select = document.querySelector('select'); select.appendChild(newOption);
The above is the detailed content of Dynamically create option in select element. For more information, please follow other related articles on the PHP Chinese website!