button tag vs input type="button" attribute - GeeksforGeeks

22 Apr, 2019
The <input type=”button”> and <button> tag are two different approach to use Button in HTML document. The difference between them is that the buttons created with the <button> tag offer many contributing possibilities than <input type=”button”> attribute. The <button> can have content inside it. The <button> tag permits phrasing content inside button element contents like text or images etc, along work with type functionality defined. But the input type=”button” attribute does not permit content. For example, a button tag that contains an image functions and may resemble an input tag whose type is set to “image”, but the button element type allows content. Both examples below very clearly shows the difference.
Note: The <button> tag uses opening and closing bracket but <input type=”button”> attribute uses only single tag.
Example 1: This example uses <input type=”button”> attribute to create button in HTML document.
<!DOCTYPE html><html>  <head>    <script>        function msg() {            alert("values submitted");        }    </script></head>      <body style = "text-align:center;">       <h1 style = "color:green;" >         GeeksForGeeks     </h1>               <h2>Using Input type</h2>           <form>        <label for="submit">            Enter value        </label>                  <input type = "text">              <input type="button" id="submit"             onclick ="msg()" value="button">     </form>     <p>        here the input type button        works properly    </p></body>  </html>                    
Output:
Before clicking the button:
After clicking the button:
Example 2: This example uses <button> tag to create button in HTML document.
<!DOCTYPE html><html>  <body style = "text-align:center;">       <h1 style = "color:green;">         GeeksForGeeks     </h1>               <h2>The Button Element</h2>      <form method="post" >        <label for="uname"><b>Username</b></label>          <input type="text" placeholder="Enter Username"                 name="uname" required>                  <br>                  <label for="psw"><b>Password</b></label>                  <input type="password" placeholder="Enter Password"                name="psw" required>                  <br>              <button type="submit"><img src=        "https://www.freeiconspng.com/uploads/login-button-png-4.jpg"            height="40" width="100">        </button><br>                  <button type="button" class="cancelbtn">Cancel</button>                  <span class="psw">Forgot <a href="#">password?</a></span>    </form></body>  </html>                    
Output:
Before clicking the button:
After clicking the button:
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
Picked
HTML
Web Technologies
Web technologies Questions
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to update Node.js and NPM to next version ?
REST API (Introduction)
How to Insert Form Data into Database using PHP ?
Types of CSS (Cascading Style Sheet)
How to position a div at the bottom of its container using CSS?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Difference between var, let and const keywords in JavaScript