Differences between html id and name attributes

Source: Internet
Author: User

This article from csdn: http://blog.csdn.net/tjvictor/archive/2007/02/11/1507572.aspx

Differences between html id and name attributes
It can be said that almost everyone who has done Web development has asked, What is the difference between the element ID and name? Why do I need a name if I have an ID ?!
We can also get the most classical answer: ID is like a person's ID card number, and name is like his name. ID is obviously unique, and name can be repeated.
Last week, I also encountered the ID and name problems. I entered an input
Type = "hidden", write only one id = 'sliceinfo', assign a value to submit, and use request. Params ["sliceinfo"] in the background. Later, I suddenly realized that the name should be used for marking, so I added a name = 'sliceinfo' to the input, and everything was okay.
The answer to ID and name in the first section is too general. Of course, that explanation is completely correct for ID. It is the identity of the HTML element on the client side. Name is actually much more complex, because name has many purposes, so it cannot be completely replaced by ID, so it is canceled. Specific uses include:
Purpose 1: ID of the server as an HTML element that can interact with the server, such as input, select, textarea, And button. On the server side, we can use request. Params to obtain the value submitted by the element based on its name.
Purpose 2: HTML element input type = 'radio' group. We know that radio
The button control is in the same group class. The check operation is mutex and only one radio can be selected at a time. This group is implemented based on the same name attribute.
Purpose 3: Create an anchor in the page. We know that link gets a page hyperlink. If you use name instead of the href attribute, for example, we get a page anchor.
Purpose 4: act as the identity of an object, such as applet,
Object, embed, and other elements. For example, in the applet object instance, we will use its name to reference this object.
Purpose 5: When associating IMG and map elements, if you want to define the IMG hotspot area, you need to use its attribute usemap to make usemap = "# name" (associated map)
Element name ).
Purpose 6: Attributes of certain elements, such as attribute, Meta, And Param. For example, defining parameters for an object or meta.
Obviously, these functions are not simply replaced by IDS, so the ID and name of the HTML element are not the same as the ID card number and name, they are actually different things.
Of course, the name attribute of the HTML element can also play an ID role on the page, because in the DHTML Object Tree, we can use document. getelementsbyname
To obtain an array of objects containing all the specified name elements on the page. There is another problem with the name attribute. When we dynamically create elements that can contain the name attribute, the value assignment element. name cannot be simply used.
= "..." To add its name. When creating an element, you must use document. createelement ('') to add the name attribute for the element. What does this mean? You can see the following example.

The message box displays the following results:
<Script language = "JavaScript">
VaR input = Document. createelement ('input ');
Input. ID = 'myid ';
Input. Name = 'myname ';
Alert (input. outerhtml );

The message box displays the following results:
<Script language = "JavaScript">
VaR input = Document. createelement ('<input name = "myname"> ');
Input. ID = 'myid ';
Alert (input. outerhtml );
The Design of initializing the name attribute is not a defect of IE, because it is mentioned in msdn. But what is the principle of this design? I do not want to be clear at the moment.
Here, by the way, if there are n (n> 1)
What should I do if the HTML element IDs are the same? How can we reference them in DHTML objects? If we use the ASPX page, this is not easy to happen, because the ASPNET process does not allow IDs when processing the ASPX page.
This is not unique. This is a page that throws an exception and cannot be used as a normal render. If we want to repeat the ID on a dynamic page, how does ie do it? At this time, we can continue to use document. getelementbyid to get objects, but we can only get objects with duplicate IDs in HTML
The first object that appears during render. At this time, duplicate IDs are automatically converted into an array when being referenced. Elements with duplicate IDs exist in the array in order of render.


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.