Home > Article > Web Front-end > Detailed explanation of the difference between ID and NAME in HTML
It can be said that almost everyone who has done web development has asked, what is the difference between the ID and Name of an element? Why do we need Name when we have ID?! And we can also get the most classical answer: ID is like a person's ID number, and Name is like his name. ID is obviously unique, and Name is repeatable.
Last week I also encountered the problem of ID and Name. I entered an input type="hidden" on the page and only wrote an ID='SliceInfo'. After assigning the value, submit and use Request in the background. Params["SliceInfo"] cannot get the value. Later, I suddenly realized that it should be marked with Name, so I added Name='SliceInfo' to the input, and everything was ok.
The answer to ID and Name in the first paragraph is too general. Of course, that explanation is completely correct for ID, which is the Identity of the HTML element on the Client side. Name is actually much more complicated, because Name has many uses, so it cannot be completely replaced by ID, thus canceling it. Specific uses are:
Purpose 1: As a server-side indicator of HTML elements that can interact with the server, such as input, select, textarea, and button. We can get the value submitted by the element through Request.Params based on its Name on the server side.
Use 2: HTML element Input type='radio' grouping, we know that radio buttoncontrol is in the same grouping class, the check operation is mutex, only one radio can be selected at the same time, this grouping is It is implemented based on the same Name attribute.
Purpose 3: Establish the anchor point in the page. We know that link is to obtain a page hyperlink. If the href attribute is not used, instead Using Name, such as: Bottom">, we get a page anchor.
Purpose 4: As the Identity of the object, 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.
Purpose 5: When associating between the IMG element and the MAP element, if you want to define the hotspot area of the IMG, you need to use its attribute usemap, so usemap="#name" (associated MAP Name of the element).
Use 6: Attributes of certain specific elements, such as attribute, meta and param. For example, define parameters for Object or in Meta.
Obviously these uses cannot be simply replaced by ID, so the difference between ID and Name of HTML elements is not the difference between ID number and name. They have different functions. .
Of course, the Name attribute of the HTML element can also play a role as an ID in the page, because in the DHTML object tree, we can use document.getElementsByName to get a list containing all the elements in the page. An array of objects specifying the Name element. There is another problem with the Name attribute. When we dynamically create an element that can contain the Name attribute, we cannot simply use the assignment element.name = "..." to add its Name. We must use document.createElement( when creating the Element. '
The result displayed in the message box is: .
The result displayed in the message box is: .
The design of initializing the Name attribute is not a defect of IE, because MSDN says it should be done this way, but what is the principle of this design? I haven't thought too clearly yet.
By the way, what if there are n (n>1) HTML elements in the page with the same ID? How to reference them in DHTML objects? If we use ASPX pages, such a situation is not easy to happen, because the aspnet process does not allow non-unique IDs when processing aspx pages. This means that the page will throw an exception and cannot be rendered normally. If it is not a dynamic page and we insist on repeating the ID, what should we do with IE? At this time, we can still use document.getElementById to obtain objects, but we can only obtain the first object that appears during HTML Render among those objects with duplicate IDs. At this time, the repeated ID will automatically become an array when referenced, and the elements with repeated IDs will exist in the array in order of Render.
The above is the detailed content of Detailed explanation of the difference between ID and NAME in HTML. For more information, please follow other related articles on the PHP Chinese website!