JS gets the value of the selected item in the dropdown box and the implementation code for the text

Source: Internet
Author: User

  This article is mainly for JS to get the value of the selected items and text implementation code is introduced, the need for friends can come to the reference, I hope to help you.

JS get the value of the dropdown box and text online provides 2 methods: But some people are very irresponsible, they did not consider the differences between the browser caused by the error, resulting in a lot of novice thinking about a half-day can not find a mistake!   Below I summarize under Firefox and IE get dropdown box to select the value and text:   1. Both IE and Firefox support methods: Get text     code as follows: Var Obj=document.getelementbyid (' select_template ');  var text= obj.options[obj.selectedindex].text;//get text     var Obj=document.getelementbyid ("Select_template");         for (i=0;i<obj.length;i++) {//dropdown box length is the number of his options            if (obj [i].selected==true) {            var text=obj[i].text;//get text        }     The previous method is relatively concise   2. IE support Firefox does not support:   code as follows: Var Obj=document.getelementbyid (name);   for (i=0;i<obj.length;i++)  {        if (obj[i].selected==true) {      & nbsp    var text= Obj[i].innertext;              {   }     Get value method ie and Firefox general: Var Value=document.gete Lementbyid ("Select_Template "). value;//Get Value   Summary: In fact, the main is IE and Firefox support the value and Text properties, Firefox does not support innertext properties.   JS implement current page open a new link:  

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.