Differences between the id and name attributes in input: _ HTML/Xhtml _ webpage Creation

Source: Internet
Author: User
I haven't figured out the difference between name and id in input till now. I have encountered this problem in my study. I 've been sorting out the collected information for a long time before I can use it as a website, however, we have not yet understood the differences between name and id in input. We have recently learned jquery and have encountered this problem. We have collected information online. After reading this article, we will sort it out for future use.

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" on the page and wrote only one ID = 'sliceinfo'. After the assignment, I submit it, use Request. params ["SliceInfo"] does not return a value. 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 the radio 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, and Embed. For example, in the Applet object instance, we will use its Name to reference this object.
Purpose 5: When the IMG and MAP elements are associated, if you want to define the IMG hotspot area, you need to use its attribute usemap, make usemap = "# name" (Name of the associated MAP element ).
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 a role of ID in the page, because in the DHTML Object Tree, we can use document. getElementsByName is used 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, we cannot simply use the value assignment element. name = "... "to add its Name, but must use document. createElement (' ') Adds the Name attribute to the element. What does this mean? You can see the following example.

The Code is as follows:


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.