HTML code:
1 </HEAD> 2 <BODY> 3 4 Book Category: 5 <select id= "S1" > 6 <option value= "1" > Teaching </option> 7 <option value= "2" > Technology </option> 8 </select> 9 </body>10
Javascrtipt Code:
Window.onload = function () { //Gets the node object of the drop-down box first; var select = document.getElementById ("S1"); 1. How do I get the currently selected value? : var value = Select.value; 2. How to get the drop-down box for all option node objects var options = select.options; Note: The resulting options are an array of objects //3. How do I get the value of the first option? For example, if I want to get the value of option one, you can do this: var value1 = options[0].value; 4. How do I get the text content of option number one? for example, I want to get the text of the first option, you can do this: var text1 = options[0].text; 5. How do I get the index of the currently selected option? var index = select.selectedindex; 6. How do I get the text content of the currently selected option? // from the 2nd question, we've got all option object array options up //and from the 5th question, we get the index value to the currently selected option ///So we just have the same options[index] The subscript method gets the currently selected option of var selectedtext = options[index].text; }
JS how to get the value of the Select drop-down box and the text content