I have been working on the website for a long time, but I haven't figured out the difference between name and id in input. Recently, I learned jquery and encountered this problem again, so I collected information online. After seeing this article, organize it for later use.
It can be said that almost everyone who has done web development has asked, what is the difference between element ID and Name? Why do we need Name with ID?! And we can also get the most classical answer: ID is like a person's ID number, and Name is like his name. The ID is obviously unique, and Name can be repeated.
Last week I also encountered the problem of ID and Name. I entered an input type=hidden on the page and wrote only an ID='SliceInfo'. After the assignment, I used Request.Params[SliceInfo] in the background but couldn't get the value. Later, I suddenly realized that I should use Name to indicate it, so I added Name='SliceInfo' to the input, and everything was OK.
The answers to ID and Name in the first paragraph are too general. Of course, that explanation is completely correct for ID. It is the Identity of the HTML element on the Client. Name is actually much more complicated because Name has many uses, so it cannot be replaced by ID, thus canceling it. Specific uses are:
Purpose 1: As a server-side label for 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 on the server side according to its Name.
Purpose 2: HTML elements Input type='radio' are grouped. We know that the radio button control is in the same grouping class. The check operation is mutex. Only one radio can be selected at the same time. This grouping is implemented based on the same Name attribute.
Purpose 3: Create an anchor in the page. We know that <a href=URL>link</a> is to obtain a page hyperlink. If we do not use the href attribute, we use Name instead, such as: <a name=PageBottom></a>, we obtain a page anchor.
Purpose 4: Identity as an object, such as Applet, Object, Embed and other elements. For example, in an Applet object instance, we will use its Name to reference the object.
Purpose 5: When associating between an IMG element and a MAP element, if you want to define the hotspot area of the IMG, you need to use its attribute usemap to make usemap=#name (name of the associated MAP element).
Purpose 6: Attributes of certain specific elements, such as attribute, meta and param. For example, define the parameter <PARAM NAME = appletParameter VALUE = value> for Object or <META NAME = Author CONTENT = Dave Raggett> in Meta.
Obviously, these uses cannot be simply replaced by IDs, so the ID of HTML elements and Name are not the difference between ID numbers and names. They are also different things.
Of course, the Name attribute of the HTML element can also play a little ID role in 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. There is another problem with the Name attribute. When we dynamically create elements that can contain Name attributes, we cannot simply use the assignment element.name = ... to add its Name. Instead, we must use document.createElement('<element name = myName></element>') to add the Name attribute to the element when creating Element. What does this mean? Just look at the example below to understand.
Copy the code