An example of the difference between the id and name attributes in input

An example of the difference between the id and name attributes in input
I have been making websites for a long time, but I still haven't figured out the difference between name and id in input. Recently, I was learning jquery and encountered this problem again, so I collected information on the Internet. When I saw this article, I sorted it out for later use.

It can be said that almost everyone who has done web development has asked, what is the difference between an element's ID and Name? Why do we need a Name if we have an ID?! We can also get the most classic answer: ID is like a person’s ID number, and Name is like his name. ID is obviously unique, while 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 only wrote an ID='SliceInfo'. After assigning the value and submitting it, I used Request.Params["SliceInfo"] in the background but could not get the value. Later I realized that I should use Name to mark it, so I added Name='SliceInfo' in 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. The Name is actually much more complicated, because the Name has many uses, so it cannot be completely replaced by the ID and thus canceled. Specific uses include:

Purpose 1: As a server-side marker for HTML elements that can exchange data 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.
Usage 2: Grouping of HTML element Input type='radio'. We know that radio button controls are in the same group class, and the check operation is mutex. Only one radio can be selected at a time. This grouping is achieved based on the same Name attribute.
Use 3: Create an anchor in the page. We know that <a href="URL">link</a> is to get a page hyperlink. If we do not use the href attribute but use Name instead, such as: <a name="PageBottom"></a>, we will get a page anchor.
Application 4: As the identity of an object, such as Applet, Object, Embed, and other elements. For example, in the Applet object instance, we will use its Name to reference the object.
Use 5: When associating an IMG element with a MAP element, if you want to define the hotspot area of ​​the IMG, you need to use its attribute usemap, so that usemap="#name" (the Name of the associated MAP element).
Usage 6: Attributes of certain specific elements, such as attribute, meta, and param. For example, define a parameter <PARAM NAME = "appletParameter" VALUE = "value"> for an Object or <META NAME = "Author" CONTENT = "Dave Raggett"> in Meta.

Obviously, these uses cannot be simply replaced by ID, so the difference between ID and Name of HTML element is not like the difference between ID number and name, they are basically things with different functions.

Of course, the Name attribute of the HTML element can also play a little role as an ID in the page, because in the DHTML object tree, we can use document.getElementsByName to get an object array containing all the specified Name elements in the page. There is another problem with the Name attribute. When we dynamically create an element that can contain a Name attribute, 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 the element. What does this mean? Take a look at the following example to understand.

Copy code
The code is as follows:

<script language="JavaScript">
var input = document.createElement('INPUT');
input.id = 'myId';
input.name = 'myName';
alert(input.outerHTML);
</script>

The result displayed in the message box is: <INPUT id=myId>.

Copy code
The code is as follows:

< script language="JavaScript">
var input = document.createElement('<INPUT name="myName">');
input.id = 'myId';
alert(input.outerHTML);
</script>

The result displayed in the message box is: <INPUT id=myId name=myName>.
The design of initializing the Name property is not a defect of IE, because MSDN says to do so, but what is the principle of this design? I haven’t figured it out yet.

By the way, what if there are n (n>1) HTML elements with the same ID on the page? How to reference them in DHTML objects? If we use ASPX pages, this situation is not likely to happen, because the aspnet process does not allow non-unique IDs when processing aspx pages. In this case, an exception will be thrown on the page and it cannot be rendered normally. If it is not a dynamic page, and we insist on repeating the ID, what should IE do? At this time, we can still continue to use document.getElementById to get the object, but we can only get the first object that appears in HTML Render among those objects with duplicate IDs. At this time, the duplicate ID will automatically become an array when referenced, and the elements with duplicate IDs will exist in the array in the order of Render.

Form elements (form input textarea select) and frame elements (iframe frame) use name
These elements are all related to form submission (frame elements act on the target of form). Only elements with names are received on the receiving page of the form. Elements with IDs cannot receive values ​​through the form. You can verify it yourself.
There is one exception: A can be assigned a name as an anchor, or an ID

Of course, the above elements can also be assigned ID values. When assigning ID values, the method of referencing these elements needs to be changed.
Assign name: document.formName.inputName document.frames("frameName")
Assign ID: document.getElementById("inputID") document.all.frameID

Elements that can only be assigned IDs but not names: (Except for form-related elements, which can only be assigned IDs)
body li table tr td th p div span pre dl dt dd font b etc.

<<:  A very detailed explanation of Linux C++ multi-thread synchronization

>>:  The latest collection of 18 green style web design works

Recommend

VMware virtual machine three connection methods example analysis

NAT In this way, the virtual machine's networ...

Detailed process of installing Presto and connecting Hive in Docker

1. Introduction Presto is an open source distribu...

CenOS6.7 mysql 8.0.22 installation and configuration method graphic tutorial

CenOS6.7 installs MySQL8.0.22 (recommended collec...

React native ScrollView pull down refresh effect

This article shares the specific code of the pull...

HTML markup language - table tag

Click here to return to the 123WORDPRESS.COM HTML ...

Detailed tutorial on installing MySQL offline on CentOS7

1. Delete the original mariadb, otherwise mysql c...

5 cool and practical HTML tags and attributes introduction

In fact, this is also a clickbait title, and it c...

Implementation steps for building a local web server on Centos8

1 Overview System centos8, use httpd to build a l...

MySQL Practical Experience of Using Insert Statement

Table of contents 1. Several syntaxes of Insert 1...

Explore VMware ESXI CLI common commands

Table of contents 【Common commands】 [Summary of c...

Windows DNS server exposed "worm-level" vulnerability, has existed for 17 years

Vulnerability Introduction The SigRed vulnerabili...

CentOS 7 set grub password and single user login example code

There are significant differences between centos7...

Mysql join query principle knowledge points

Mysql join query 1. Basic concepts Connect each r...

The use of textarea in html and common problems and case analysis

The textarea tag is an HTML tag that we often use....