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);
- Here, we have created an Element using “document.createElement” function and stored it in “btn” variable.
- Then we set text for button using JS innerText() function
- And We have printed both of structure and text of the button
Expected Output: