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.

  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    

Load document on the event!

  document.addEventListener ("load", function () {var allButtons = document.getElementsByTagName ('button'); for (i = 0; I    Update   

Ok you can do something like this

  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

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -