Solve the problem that radio created through createelement cannot be selected.

Source: Internet
Author: User

During the development process, you need to use js to dynamically add radio to the page, and use the document. createelement () method to implement it.CodeAs follows:

 

Code
VaR _ Radio = Document. createelement ( " Input " );
_ Radio. Type =   " Radio " ;
_ Radio. Name =   " _ Radio " ;
Document. Body. appendchild (_ radio );
_ Radio = Document. createelement ( " Input " );
_ Radio. Type =   " Radio " ;
_ Radio. Name =   " _ Radio " ;
Document. Body. appendchild (_ radio );

The Raido generated on ie cannot be selected, but Firefox can be selected. Then I tried the following code:

Code
VaR _ Radio = Document. createelement ( " <Input type = 'Radio 'name = '_ Radio'> " );
Document. Body. appendchild (_ radio );
_ Radio = Document. createelement ( " <Input type = 'Radio 'name = '_ Radio'> " );
Document. Body. appendchild (_ radio );

The radio generated on IE can be selected, but it is invalid in Firefox. Therefore, different browsers need to use different methods to generate radio. This can be achieved by determining whether the document has the uniqueid attribute, because uniqueid is a unique attribute of IE, to solve this problem, you can use the following code to ensure browser compatibility:

 

Code
If (Document. uniqueid ){
// IE browser Branch
VaR _ Radio = Document. createelement ( " <Input type = 'Radio 'name = '_ Radio'> " );
Document. Body. appendchild (_ radio );
_ Radio = Document. createelement ( " <Input type = 'Radio 'name = '_ Radio'> " );
Document. Body. appendchild (_ radio );
} Else {
// Non-IE browser Branch
VaR _ Radio = Document. createelement ( " Input " );
_ Radio. Type =   " Radio " ;
_ Radio. Name =   " _ Radio " ;
Document. Body. appendchild (_ radio );
_ Radio = Document. createelement ( " Input " );
_ Radio. Type =   " Radio " ;
_ Radio. Name =   " _ Radio " ;
Document. Body. appendchild (_ radio );
}

In addition, radio and checkbox generated through document. createelement ("input") cannot be obtained through the document. getelementsbyname () method in IE browsing.

Abstract: http://hotpepper.javaeye.com/blog/236338

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.