button in vanilla js 1

button in vanilla js

let newButton = document.createElement('button');
newButton.setAttribute("id", "myNewButton");
newButton.className = "myClassName";
newButton.innerText = "clickMe";
newButton.addEventListener("click", clickMe);
document.body.appendChild(newButton);
function clickMe(){
    console.log("Hi! I  am a new Button.");
}

Here is what the above code is Doing:
1. Create a new button element
2. Set the id attribute of the button to “myNewButton”
3. Set the class attribute of the button to “myClassName”
4. Set the innerText of the button to “clickMe”
5. Add an event listener to the button that will call the clickMe function when the button is clicked
6. Add the button to the body of the page
7. Define the clickMe function

Similar Posts