How to get the text of option selected in the following box in Firefox

Source: Internet
Author: User

There is no innertext in Firefox, so it is difficult to obtain the option text (not the value) in Firefox. I will summarize the solutions and code in my project. Please advise.

Knowledge point:

0. Why innertext? Security problems

1. Extend attributes for the Firefox Dom Model

2. The currentstyle attribute can get the actual style status.

3. The display mode is taken into account when ie implements innertext. If it is block, a line break is added.

4. Why not use textcontent? Because textcontent does not consider the display method of elements, it is not fully compatible with IE.

Code:All tests passed in IE6, 7,8, and Firefox 2 and 3.

 

<HTML>
<Head>
<Script language = "JavaScript">

// If your browser is IE, return true. If is others, like Firefox, return false.
Function isie () {// ie?
If (window. Navigator. useragent. tolowercase (). indexof ("MSIE")> = 1)
Return true;
Else
Return false;
}

// If is Firefox, We need to rewrite its innertext attribute.
If (! Isie () {// Firefox innertext define
Htmlelement. Prototype. _ definegetter _ ("innertext ",
Function (){
VaR anystring = "";
VaR Childs = This. childnodes;
For (VAR I = 0; I <Childs. length; I ++ ){
If (Childs [I]. nodetype = 1)
Anystring + = Childs [I]. tagname = "Br "? '\ N': Childs [I]. textcontent;
Else if (Childs [I]. nodetype = 3)
Anystring + = Childs [I]. nodevalue;
}
Return anystring;
}
);
Htmlelement. Prototype. _ definesetter _ ("innertext ",
Function (stext ){
This. textcontent = stext;
}
);
}

Function getselectedtext (name ){
VaR OBJ = Document. getelementbyid (name );
For (I = 0; I <obj. length; I ++ ){
If (OBJ [I]. Selected = true ){
Return OBJ [I]. innertext;
}
}
}

Function chk (){
VaR objtext = getselectedtext ("myselect ");
Alert ("seleted option's text is:" + objtext );

VaR objvalue = Document. getelementbyid ("myselect"). value;
Alert ("seleted option's value is:" + objvalue );
}
</SCRIPT>
</Head>
<Body>
<Select id = "myselect">
<Option value = "1111"> my 1111 hahaha </option>
<Option value = "2222"> my 2222 </option>
<Option value = "3333"> my 3333 </option>
<Option value = "4444"> my 4444 </option>
</SELECT>
<Input type = "button" name = "button" value = "see result" onclick = "javascript: chk ();">
</Body>
</Html>

Of course, if you are solely targeting the drop-down box, you do not need to rewrite innertext. The following code can also be used. Innertext is rewritten to be compatible with HTML elements other than the drop-down box.

<HTML>
<Head>
<Script language = "JavaScript">

Function chk (){
// Var objtext = getselectedtext ("myselect ");
VaR OBJ = Document. getelementbyid ("myselect ");
VaR objtext = obj. Options [obj. selectedindex]. Text
Alert ("seleted option's text is:" + objtext );

VaR objvalue = Document. getelementbyid ("myselect"). value;
Alert ("seleted option's value is:" + objvalue );
}
</SCRIPT>
</Head>
<Body>
<Select id = "myselect">
<Option value = "1111"> my 1111 hahaha </option>
<Option value = "2222"> my 2222 </option>
<Option value = "3333"> my 3333 </option>
<Option value = "4444"> my 4444 </option>
</SELECT>
<Input type = "button" name = "button" value = "see result" onclick = "javascript: chk ();">
</Body>
</Html>

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.