Deep Learning HTML: The difference between the ID and the name attribute

Source: Internet
Author: User
Tags array object reference

It can be said that almost every person who has done web development has asked, exactly what is the difference between an element's ID and a name? Why do I have ID and name?! And also we can get the most classical answer: ID is like a person's ID number, and name is like his name, ID is clearly unique, and name can be repeated.

Last week I also encountered the ID and name of the problem, in the page entered an input type= "hidden", only wrote a id= ' Sliceinfo ', assigned after submit, in the background with request. params["Sliceinfo"] but how can not go to the value. Later it dawned because the name to mark, so in the input Riga a name= ' Sliceinfo ', all OK.

The first paragraph is too general for the ID and name solution, and of course that explanation is completely right for the ID, which is the identity of the client-side HTML element. And name is actually a lot more complicated, because name has many uses, so it can't be completely replaced by ID, so it's canceled. Specific uses are:

Usage 1: As the server side of the HTML element that can interact with the server, such as input, select, TextArea, and button. We can get the value submitted by the element via Request.params on the server side according to its name.
Use 2:html element input type= ' Radio ' group, we know radio button control in the same grouping class, check operation is a mutex, the same time can only select a radio, this group is based on the same name property to achieve.
Usage 3: Establish anchor points in the page, we know <a href= "URL" >link</a> is to get a page hyperlink, if not href attribute, instead of name, such as: <a name= "Pagebottom" > </a>, we get a page anchor point.
Use 4: As an object of identity, such as applet, object, embed and other elements. For example, in an Applet object instance, we will use its name to refer to the object.
Usage 5: When associating an IMG element with a MAP element, if you want to define an IMG hotspot area, you need to use its property usemap to make the usemap= "#name" (the name of the associated map element).
Usage 6: Properties of certain elements, such as Attribute,meta and Param. For example, for object definition parameter <param name = "Appletparameter" value = "value" > or META <meta name = "Author" CONTENT = "Dave Raggett" >.

Obviously these uses are not simple to use the ID to replace, so the HTML element ID and name is not the difference between ID number and name, they are different things.

Of course the name attribute of the HTML element can also play a bit of ID on the page, because in the DHTML object Tree we can use Document.getelementsbyname to get an array of objects containing all the specified name elements in the page. Another problem with the Name property is that when we dynamically create an element that can contain the Name property, we cannot simply add its name using the assignment = "..." and must use Document.createelement ( ' <element name = ' myname ' ></element> ' adds the name attribute to the element. What does that mean? Take a look at the example below to see.

<script language= "JavaScript" >
var input = document.createelement (' input '); = ' myId '; = ' myname ';
alert (input.outerhtml);

The result displayed in the message box is: <input id=myid>.

<script language= "JavaScript" >
var input = document.createelement (' <input name= ' myname ' > '); = ' myId ';
alert (input.outerhtml);

The result displayed in the message box is: <input Id=myid name=myname>.

The design for initializing the Name property is not a flaw in IE, because MSDN says so, but what is the rationale for this design? I don't want to know for a moment.

By the way, what if the ID of the N (n>1) HTML element in the page is the same? How do you reference them in a DHTML object? This is not easy to do if we use an ASPX page, because the ASPNET process does not allow an ID to be unique when it processes an ASPX page, which is that the page is thrown out of the ordinary render. If the dynamic page, we want to let ID repeat that ie how to do it? At this point we can continue to use document.getElementById to get objects, except that we can only get objects whose IDs are duplicated in those objects that appear first in HTML render. The duplicate ID will automatically become an array at the time of reference, and the elements with the duplicate ID are in the array in render order.

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: 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.