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);
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3