JS gets a simple instance of the value and text values of the select

  • 2020-03-30 02:09:47
  • OfStack


<select id = "cityList" >   
<select id = "selectId" >   
   <option value = "0"> The first 0 a </option>   
</select>   

<script>   
    var selectObj = document.getElementById('selectId');   
    //Add option  by object;  
    selectId.add(new Option(" The first one ","1"))   
    selectId.add(new Option(" The second ","2"))   
    //Add option&cake by id;  
    selectId.add(new Option(" The third ","3"))   
    selectId.add(new Option(" The fourth ","4"))   
    //Add method by id (or by object) & PI;  
    selectId.onchange = function(){   
        //Gets the value and the text&cake through the object;  
        alert(selectObj.value);   
        alert(selectObj.options[selectObj.selectedIndex].text);   
        //Get the value and the text&cake by id;  
        alert(selectId.value);   
        alert(selectId.options[selectId.selectedIndex].text);   
        //This can also be used to get the value and the text&cake;  
        alert(this.value);   
        alert(this.options[this.selectedIndex].text);   
    };   
</script>  

Related articles: