JS dynamically adds and removes the sample code for the Option object of in the select

  • 2020-03-30 01:00:12
  • OfStack

As follows:

//Dynamically deletes all options: & NBSP; in the select;  
function delAllOptions(){  
      document.getElementById("user_dm").options.length=0;   
}  

//Dynamically deletes an option in the select: & NBSP;    
function delOneOption(index){  
      document.getElementById("user_dm").options.remove(index);   
}  
//Dynamically add option in select :& NBSP;    
function addOneOption(){  
      //document.getElementById("user_dm").options.add(new Option(2,"mytest"));    

   var selectObj=document.getElementById("user_dm");  
   alert(selectObj.length);  
   selectObj.options[selectObj.length] = new Option("mytest", "2");  
}  

Related articles: