JS how to get the value of the Select drop-down box and the text content

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.