button tag vs input type=”button” attribute

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.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:

  • Before clicking the button:
  • After clicking the button:

Example 2: This example uses <button> tag to create button in HTML document.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:

  • Before clicking the button:
  • After clicking the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.