Create Your First HTML Button: Simple Tutorial with Example

First We Will create the most simple button with html without any functionality. That means it will just show the button and do nothing..



<!DOCTYPE html>
<html>
<head>
</head>
<body>

<button>Button</button>

</body>
</html>


    
    
    

From the code.

  • We set the button with button tag like this <button>Button Text</button>

This code is missing the functionality. That means we did not said the code when the user click on the button what should it do!

So in the next example we will add this functionality.

It will be like this. When the user click the button it will show this text “Hello User”



<!DOCTYPE html>
<html>
<head>
</head>
<body>

<button id="HelloButton">Button</button>
<p id="message"></p>

<script>
  var exampleButton = document.getElementById('HelloButton');

  exampleButton.addEventListener('click', function() {
    var messageParagraph = document.getElementById('message');
    messageParagraph.innerHTML = 'Hello User';
  });
</script>

</body>
</html>





    
    
    

From The code

When the user click the button it triggers the JavaScript function and find the message id and include the text inside the paragraph tag.

In the Next Example We added some css of button.

Here the color means the font color

Now add some design in the button




<!DOCTYPE html>
<html>
<head>
  <style>

    #HelloButton {
      padding: 10px 20px;
      background-color: black;
      color:white;
      font-size: 16px;
    }

    #message {
      font-size: 18px;
      margin-top: 10px;
    }
  </style>
</head>
<body>

<button id="HelloButton">Button</button>
<p id="message"></p>

<script>
  var button = document.getElementById('HelloButton');
  var messageParagraph = document.getElementById('message');

  button.addEventListener('click', function() {
    messageParagraph.innerHTML = 'Hello User';
  });
</script>

</body>
</html>



    
    
    

Test the code here. Excersise the example instantly..

Related Posts

How to HTML open link in new in tab in browser
July 3, 2024

We can add this target=”_blank” in the <a> tag to open link in new tab Example code <!DOCTYPE html> <head> </head> <body> &lt!– Opens in the same tab –> <a href=”https://en.wikipedia.org/wiki/Main_Page”>Visit Wikipedia (same tab)</a> <br> &lt!– Opens in a new tab –> <a href=”https://en.wikipedia.org/wiki/Main_Page” target=”_blank”>Visit Wikipedia (new tab)</a> </body> </html> Copy Code

Change button text with jQuery
July 3, 2024

Its essential to change t he button text in some applications . For example Like button. If the current is “Like”, after click on the button it the button text will be “Liked” jquery makes process efficient. <!DOCTYPE html> <html> <head> <script src=”https://code.jquery.com/jquery-3.7.1.min.js”></script> </head> <body> <button class=”like-button”>Like</button> <script> $(document).ready(function(){ $(‘.like-button’).click(function(){ var buttonText = $(this).text(); if […]

How to prevent text selection in html
April 28, 2024

Its necessary to prevent text selection when the text is ui feature, to make the webpage more professional. We can prevent the text from selecting by using this in css You cannot select this text You can select this text example code