To detail the difference between the ID of an HTML element and the Name property

Source: Internet
Author: User
Tags define array object reference
Difference

Almost everyone 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 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 that the radio button control is in the same grouping class, the check operation is a mutex, only one radio can be selected at a time, which is based on the same name attribute.

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.

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: attributes of certain elements, such as attribute, and Param. For example, define a parameter for object <param NAME = "Appletparameter" value = "value" >.

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.

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