Open In App

button tag vs input type=”button” attribute

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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=
            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:


Last Updated : 22 Apr, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads