- HTML Tutorial
- HTML - Home
- HTML - History and Evolution
- HTML - Overview
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Headings
- HTML - Paragraphs
- HTML - Quotations
- HTML - Comments
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Style Sheet
- HTML - CSS Classes
- HTML - CSS IDs
- HTML - Images
- HTML - Image Map
- HTML Tables
- HTML - Tables
- HTML - Headers & Caption
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Iframes
- HTML - Blocks
- HTML Backgrounds
- HTML - Backgrounds
- HTML Colors
- HTML - Colors
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Code Builder
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - name Attribute
The HTML name attribute is used to specify the name of an element. It is used by the server to identify fields in form submissions.
The name attribute value appears on the URL with their respective name after form submission. If the name attribute is not specified within the form controls or is an empty string then no form data will appear on the URL.
The name attribute works with the following tags −
<button>, <form>, <fieldset>, <iframe>, <input>, <object>, <output>, <select>, <textarea>, <map>, <meta>, <param>.
Syntax
Following is the syntax of name attribute −
<tag name= "value"></tag>
Example
In the following example, we are using the HTML 'name' attribute within the <button> tag to specify its name.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML 'name' attribute</title> <style> button { width: 150px; padding: 10px; background-color: blueviolet; color: white; cursor: pointer; } </style> </head> <body> <!--HTML 'name' attribute--> <p>Example of the HTML 'name' attribute</p> <p>Click on the below button to get button name attribute value.</p> <button name='submit' onclick="Get()" id='btn'>Display Button Name</button> <script> function Get() { let btn = document.getElementById('btn'); let btn_name = btn.getAttribute('name'); alert("Button name: " + btn_name); } </script> </body> </html>
When we execute the above script, it will generate an output consisting of the text along with a click button on the webpage. when the user clicks the button the event gets triggered and displays an alert.
Example
Consider the another scenario, where we are going to use the name attribute with the input tag.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML 'name' attribute</title> <style> input { width: 200px; padding: 10px; } button { width: 150px; padding: 10px; background-color: blueviolet; color: white; cursor: pointer; } </style> </head> <body> <!--HTML 'name' attribute--> <p>Example of the HTML 'name' attribute</p> <h3>User Login Page</h3> <form method="GET"> <h2>Login</h2> <label for="">Username</label> <br> <input type="text" name='uname'> <br> <br> <label for="">Password</label> <br> <input type="password" name='password'> <br> <br> <button>Login</button> </form> <p>After Clicking on the above button, the form input value will appear with the name on the URL.</p> </body> </html>
On running the above code, the output window will pop up displaying the input field along with a click button displayed on the webpage.
Example
Let's look at the following example, where we are going to use the name attribute with the select tag.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML 'name' attribute</title> <style> select { width: 200px; padding: 6px; } </style> </head> <body> <!--HTML 'name' attribute--> <p>Example of the HTML 'name' attribute</p> <p>Choose language you knows: </p> <form method="GET"> <select name="languages" id="demo"> <option value="">Choose you language</option> <option value="Hindi">Hindi</option> <option value="English">English</option> <option value="Telugu">Telugu</option> </select> <button>Submit</button> </form> </body> </html>
When we run the above code, it will generate an output consisting of the dropdown mwnu along with a click button displayed on the webpage
Example
Following is the example, where we are going to use the name attribute with the text area tag.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML 'name' attribute</title> <style> select { width: 200px; padding: 6px; } </style> </head> <body> <!--HTML 'name' attribute--> <p>Example of the HTML 'name' attribute</p> <p>Write your feedback in the below field and submit by clicking on submit button.</p> <form method="GET"> <label for="">Your feedback....</label> <br> <textarea name="yourfeedback" cols="30" rows="8"></textarea> <br> <button>Submit</button> </form> </body> </html>
On running the above cocde, the output window will pop up displaying the textarea field filled with data along with a click button on the webpage.
To Continue Learning Please Login
Login with Google