create button javascript code example

Example 1: how to create a button with javascri[t

// 1. Create the button
var button = document.createElement("button");
button.innerHTML = "Do Something";

// 2. Append somewhere
var body = document.getElementsByTagName("body")[0];
body.appendChild(button);

// 3. Add event handler
button.addEventListener ("click", function() {
  alert("did something");
});

/* Read 

https://css-tricks.com/use-button-element/
*/

Example 2: how to add button in javascript

const button = document.createElement("button");
button.innerHTML = "Do Something";

Example 3: create link and click javascript

const link = document.createElement('a');
link.id = 'someLink'; //give it an ID!
link.href = 'https://example.com'; // Your URL

//Add the link somewhere, an appendChild statement will do.
//Then run this
document.getElementById('someLink').click();

Example 4: how to make a button in javascript

<button id="btn">button</button>

Tags: