difference between name and ID in the input box _javascript tips

Source: Internet
Author: User

1.

It can be said that almost every person who has done web development has asked, exactly what is the difference between an element's ID and a name? Why do I have ID and name?! And also we can get the most classical answer: ID is like a person's ID number, and name is like his name, ID is clearly unique, and name can be repeated.

Last week I also encountered the ID and name of the problem, in the page entered an input type= "hidden", only wrote a id= ' Sliceinfo ', assigned after submit, in the background with request. params["Sliceinfo"] but how can not go to the value. Later it dawned because the name to mark, so in the input Riga a name= ' Sliceinfo ', all OK.

The first paragraph is too general for the ID and name solution, and of course that explanation is completely right for the ID, which is the identity of the client-side HTML element. And name is actually a lot more complicated, because name has many uses, so it can't be completely replaced by ID, so it's canceled. Specific uses are:

Usage 1: As the server side of the HTML element that can interact with the server, such as input, select, TextArea, and button. We can get the value submitted by the element via Request.params on the server side according to its name.

Use 2:html element input type= ' Radio ' group, we know radio button control in the same grouping class, check operation is a mutex, the same time can only select a radio, this group is based on the same name property to achieve.

Usage 3: Establish anchor points in the page, we know <a href= "URL" >link</a> is to get a page hyperlink, if not href attribute, instead of name, such as: <a name= "Pagebottom" > </a>, we get a page anchor point.

Use 4: As an object of identity, 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.

Usage 5: When associating an IMG element with a MAP element, if you want to define an IMG hotspot area, you need to use its property usemap to make the usemap= "#name" (the name of the associated map element).

Usage 6: Properties of certain elements, such as Attribute,meta and Param. For example, for object definition parameter <param name = "Appletparameter" value = "value" > or META <meta name = "Author" CONTENT = "Dave Raggett" >.

Obviously these uses are not simple to use the ID to replace, so the HTML element ID and name is not the difference between ID number and name, they are different things.

The form element (form input textarea Select) is used with the frame element (IFrame frame) with Name

These elements are related to the submission of the form (the frame element acts on the target of form), where only the element with name is received on the form's receiving page, and the assigned ID element receives no value through the form, and you can verify it yourself.

There is an exception: A can assign name as an anchor point, or assign an ID.

2.

IDS should conform to identity requirements, such as case sensitivity, preferably not underlined (because CSS is not compatible). and name basically does not have any requirements, even can use the number. ID is like a person's ID number, and name is like his name, the ID is clearly unique, and name can be duplicated.

<input> labels Specify the input fields that users can enter data into.

Depending on the type attribute, the input field has several forms. The input fields can be text fields, check boxes, password fields, radio buttons, buttons, and so on.
ID: A unique identification of the label.

Name: The server-side identifier of the HTML element that can interact with the server.

3.

ID is in #定义的CSS样式

You can also use JS acquisition to control getElementById (here is the ID) to get

Name a lot, is to give the current tag or element name, you can use JS to control the value, form submitted after the acquisition of the need to get name name.

4.

IDs are commonly used in CSS and JS references, name is used for form submission, and only tagged elements with the name attribute are submitted to the server.

The above is a small set to introduce the input box in the name and ID of the difference, I hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.