DEV Community

Cover image for How to Create a Button in JavaScript
Gaël Thomas for HereWeCode

Posted on • Originally published at herewecode.io

How to Create a Button in JavaScript

Learn how to create a button in JavaScript and add it to your HTML page.


As a web developer, you should probably create buttons and add them to your HTML page programmatically.

A typical workflow is to validate some data in your JavaScript code before displaying a button in HTML to your users.

Before starting, let me show you what you will achieve!

<button type="button">Can you click me?</button>
Enter fullscreen mode Exit fullscreen mode

HTML button in JS

When you click on the button, your JavaScript code will trigger an alert.

Button in JavaScript with on click Alert

Let's dive into that tutorial!

1. Create a button using document createElement

To create a button in JavaScript, you must use the createElement function available by default in the Document interface of your web page.

Then, you will configure the button with a "Can you click me?" text.

// Create a button element
const button = document.createElement('button')

// Set the button text to 'Can you click me?'
button.innerText = 'Can you click me?'
Enter fullscreen mode Exit fullscreen mode

As a side note, you can add a lot of properties to a button when creating it. This tutorial only sets the innerText because we don't need the other properties.

2. Add an event listener on your button

The next step is to create an event listener on your button. It will allow you to detect when a user clicks on it.

Whenever the "click" event is detected, the arrow function (second parameter) is launched.

// Attach the "click" event to your button
button.addEventListener('click', () => {
  // When there is a "click"
  // it shows an alert in the browser
  alert('Oh, you clicked me!')
})
Enter fullscreen mode Exit fullscreen mode

3. Add your JS button to your HTML

The last step is to add your button to the HTML body.

After that, you will have the same result as the beginning example!

// Add the button to your HTML <body> tag
document.body.appendChild(button)
Enter fullscreen mode Exit fullscreen mode

Full code to create a button in JS 👇

<html>
  <head>
    <title>Create a Button in JavaScript</title>
  </head>

  <body>
    <!-- Empty <body> HTML (the JavaScript code in <script> will generate a button here)-->

    <script>
      // Create a button element
      const button = document.createElement('button')

      // Set the button text to 'Can you click me?'
      button.innerText = 'Can you click me?'

      button.id = 'mainButton'

      // Attach the "click" event to your button
      button.addEventListener('click', () => {
        // When there is a "click"
        // it shows an alert in the browser
        alert('Oh, you clicked me!')
      })

      document.body.appendChild(button)
    </script>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

What's next?

Congrats! 🎉 Now you know how to programmatically create buttons using JavaScript. You can go a bit further by learning how to enable and disable a button in JavaScript.


➡️ I'm starting to tweet more consistently. If you want to get more tips and resources about web development, developer tips, and my journey as a Full-stack Engineer -> Find me on my Twitter 🐦

Top comments (2)

Collapse
 
noorpari98 profile image
noorpari98 • Edited

Can I Share my own Code with Advanced Button Coding, When a user hover on that It shows press me when the user press it shows happy emoji instead of button.

Code GURU: JBBATTERY

Collapse
 
gaelgthomas profile image
Gaël Thomas

For sure, you can share your code! It can be a good next step to the article. Do you have a JSFiddle, a CodePen link, or any other? 😊

Did you already think of creating an article about how to make that kind of button?