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.

html_attributes_reference.htm
Advertisements