YouTip LogoYouTip

Att Form Name

# HTML
name Attribute The `name` attribute of the `` tag is used to specify a unique identifier for an HTML form. This identifier is primarily used to reference the form in JavaScript or to identify the form data after submission. --- ## Definition and Usage The `name` attribute defines the name of an HTML form. ### Why use the `name` attribute? 1. **JavaScript Reference:** It allows developers to easily target and manipulate the form using the Document Object Model (DOM) via `document.forms` or simply `document.formName`. 2. **Form Submission:** It helps identify which form was submitted when multiple forms exist on a single page. --- ## Browser Support The `name` attribute is fully supported by all major modern web browsers: * Google Chrome * Mozilla Firefox * Microsoft Edge / Internet Explorer * Safari * Opera --- ## Syntax ```html ``` ### Attribute Values | Value | Description | | :--- | :--- | | *text* | Specifies the name of the form. It should be a unique string within the document. | --- ## HTML 4.01 vs. HTML5 Differences There are no differences in how the `name` attribute functions between HTML 4.01 and HTML5. ## HTML vs. XHTML Differences In XHTML, the `name` attribute is **deprecated**. Developers should use the global `id` attribute instead to uniquely identify the form element. --- ## Code Examples ### Example 1: Referencing a Form in JavaScript The following example demonstrates how to use the `name` attribute to reference and submit a form using JavaScript. ```html



``` ### Example 2: Accessing Form Elements Directly When a form has a `name` attribute, you can also access its child input elements directly through the form object in JavaScript: ```javascript // Access the input element named "fname" inside the form named "myForm" let firstNameValue = document.myForm.fname.value; console.log(firstNameValue); ``` --- ## Best Practices and Considerations * **Uniqueness:** While not strictly enforced by browsers, the `name` value should be unique within the HTML document to avoid conflicts when referencing elements in JavaScript. * **Name vs. ID:** * Use `id` for CSS styling and general DOM manipulation (`document.getElementById()`). * Use `name` for form submission data handling and legacy DOM form collections (`document.forms`). * **Avoid Reserved Words:** Do not use JavaScript reserved keywords (such as `submit`, `reset`, or `history`) as the value for the `name` attribute, as this can overwrite built-in form methods and cause unexpected scripting errors.
← Att Form NovalidateAtt Form Method β†’