How to Create a Button in JavaScript | Easy Way

How to Create a Button in JavaScript

Learn how to create a button in JavaScript with simple instructions:

Method 1: Using Tag

document.body.innerHTML = '<button>New Button </button>';

Here, we have created Button using <button> tag.  We have written the code “document.body.innerHTML” to print in JavaScript

Method 2: Using the JS function

const btn = document.createElement('button');
btn.innerText = 'New Button';
document.body.appendChild(btn);
  1. Here, we have created an Element using “document.createElement” function and stored it in “btn” variable.
  2. Then we set text for button using JS innerText() function
  3. And We have printed both of structure and text of the button

Expected Output:

how to create a button in javascript

Leave a Reply