Home Web Front-end HTML Tutorial The difference between ID and NAME in HTML

The difference between ID and NAME in HTML

Apr 05, 2017 am 09:43 AM
html id name the difference

It can be said that almost everyone who has done web development has asked, what is the difference between the ID and Name of an element? Why do we need Name when we have ID? And 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, and Name can be repeated.

The answer to ID and Name in the above paragraph is too general. Let’s discuss it in detail below.

1. Both name and id can be used to call elements in html

When we need to access an html element in the page in a script, we usually specify the id or name of the HTML element

Under the WEB standard, any element in the HTML document can be accessed through getElementById(), getElementsByName(), and getElementsByTagName().

To access a specific element try to use the standard document.getElementById(id).

If there are more than two tags with the same name in a document, then document.getElementsByName(name) can obtain these elements to form an array.

To access tags, use the standard document.getElementByTagName(tag).

Note: The ID identifier is case-sensitive, while the name basically has no requirements and can even use numbers.

2. Under what circumstances must name be used?

1. Form elements (for example: form, input, textarea, select, button, etc.) are usually related to form submission. On the receiving page of the form, only elements with names are received. Elements assigned with ids cannot receive values ​​through the form.

2. The form elements checkbox and radio button usually correspond to multiple controls. In this case, the name attribute must be used to achieve grouping. The same group uses the same name. There will be no interference between the options of the two questions.

3. The name of the frame element (iframe, frame), used to specify targets in other iframes and frames.

4. Establish the anchor point in the page. We know that link is to obtain a page hyperlink. If the href attribute is not used, name is used instead, such as: , we get a page anchor.

5. When linking IMG elements and MAP elements, if you want to define the hotspot area of ​​IMG, you need to use its attribute usemap, so usemap="#name" (the name of the associated MAP element).

6. Attributes of certain specific elements, such as attribute, meta and param. For example, define parameters for Object or in Meta

3. Under what circumstances must id be used?

1. The association between label and form control. For example:

<label for="myInput">文本录入框</label>
<input id="myInput" type="text" />
Copy after login

2. ID can be used to set a css style. For example:

<style type="text/css">
#my
{
      font-size: 12px;
      color: red;
}
</style>
<p id="my">设置字体大小和颜色</p>
Copy after login

         

The above is the detailed content of The difference between ID and NAME in HTML. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to set password protection for export PDF on PS How to set password protection for export PDF on PS Apr 06, 2025 pm 04:45 PM

Export password-protected PDF in Photoshop: Open the image file. Click "File"&gt; "Export"&gt; "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Apr 05, 2025 pm 01:03 PM

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

How to use XPath to search from a specified DOM node in JavaScript? How to use XPath to search from a specified DOM node in JavaScript? Apr 04, 2025 pm 11:15 PM

Detailed explanation of XPath search method under DOM nodes In JavaScript, we often need to find specific nodes from the DOM tree based on XPath expressions. If you need to...

The Role of HTML: Structuring Web Content The Role of HTML: Structuring Web Content Apr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

See all articles