The difference between ID and name in HTML

Source: Internet
Author: User

In HTML: name refers to the user name, and the ID refers to the user registration is a serial number that the system automatically assigns to the user.

Name is used to submit data, provided to the table alone, can be repeated;

The ID is used for document operation and cannot be duplicated. such as: document.getElementById ();

First, the ID is used in the client script! Name is used to get the form field information for the submission form, which is not sent to the server side if name is not specified in the form.

Second, the following elements input, select, form, frame, iframe with name, and the following elements table, TR, TD, Div, p, span, H1, Li with id, form element (form input TEXTAREA Select) With the frame element (IFrame frame) with name, these elements are related to the form (the frame element acts on the target of the form), and only the element with name is received on the receiving page of the form, and the element that assigns the ID is not receiving the value through the form.
Of course, the above elements can also assign ID values, the method of referencing these elements when assigning ID values will change.
Fu Name:document.formName.inputName document.frames ("FrameName")
Fu ID:document.all.inputID Document.all.frameID
You can only assign an element with an ID that cannot be assigned a name: (except that the element associated with the form can only be assigned an ID)
Body Li a table tr TD th P Div span pre DL DT DD font B et cetera

To give a simple example
<form name= "Form1" >
User name: <input type=text name= "username" id= "username" >
Password: <input type=password name= "password" id= "pwd" >
</form>
If I want to get a username and password; JS with the name obtained, you have to write document.form1.username.value;
Document.form1.password.value;
Get by ID:
Docuement.getelementbyid ("username");
Docuement.getelementbyid ("pwd");
Sometimes name may appear the same name, so we can not determine which value is obtained by using name.
Document.getelemntsbyname ("username");
Here we get an array.


Document.getelementsbyname (XXX); The array taken out
document.getElementById (XXX); Take out is a single object


There's something to be aware of.

Some elements, such as Div, can get an object directly with an ID name.
<div id= "Div1" ></div>
div1.innerhtml = "ASDFASDF";

And the form element is not allowed to do that.
Only with document.getElementById (XX)

The difference between ID and name in HTML

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.