javascript - How do I add a click event for buttons when the page loads? -
I want to add click event for all buttons after loading on page, I tried to 'click' it 'Onload' but it was not working so I should do something wrong. AddClickEvent is a function that adds different click events for the button so that they do different things depending on the button you click. So, how can I work it properly?
Edit : This may be possibly unclear, but if I want an applied adclick event when it loads on the page, otherwise it will repeat the buttons for the desired effect. Load document on the event! Ok you can do something like this
var allButtons = document.getElementsByTagName ('button'); For (i = 0; i & lt; allButtons.length; i ++) {allButtons [i] .addEventListener ('click', addClickEvent); } AllButtons [0] .removeEventListener ('click', addClickEvent); // This is the first button
document.addEventListener ("load", function () {var allButtons = document.getElementsByTagName ('button'); for (i = 0; I
window.addEventListener ("load" Function () {var allButtons = document.getElementsByTagName ('button'); allButtons.addEventListener ('click', addClickEvent); All buttons [0] .removeEventListener ('click', addClickEvent);}); Function addClickEvent (evt) {switch (evt.toElement.value)) {"edit": // your code break; "Delete": // Your code break; "Completed": // Your code break; Default: console.log ("Error"); break; }}
Comments
Post a Comment