JS to HTML tag select to get add delete operations

  • 2020-03-26 21:29:44
  • OfStack

 
<SELECT NAME="aaa" SIZE="" style="width:200px" > 

<OPTION VALUE="">( empty )</OPTION> 
<OPTION VALUE="1">1</OPTION> 
</SELECT> 

 
//Get the HTML control
var select = document.getElementsByName("aaa")[0]; 

select.selectedIndex = index; 
//Create a new Option object
new Option(text,value) 
new option(text,value,defaultSelected,selected 
text: String, specify option The object's text attribute ( namely <option></option> Between words ) 
value: String, specify option The object's value attribute  
defaultSelected: Boolean value, specified option The object's defaultSelected attribute  
selected: Boolean value, specified option The object's selected attribute  

//Add options to select
select.add(new Option(text,value)) 

// delete  
select.options.remove(index) 
//Delete all at once
select.length = 0; 

Related articles: