Wednesday 15 February 2012

javascript - How do I get a created element (a button) to run a function? -


This is the code I used to make code:

  var dfom = document CreateElement ('div'); Dfom.setAttribute ("id", "options"); Dfom.innerHTML = "& lt; input type = \" button \ "value = \" hi hi \ "onclick = \ get () \ />"; Dfom.style.padding = '10px'; Document.body.insertBefore (dfom, document.body.firstChild);   

The button has been made fine; To display it correctly, I did this:

 get the  function () {alert ("HI"); }   

However, when I click on the button, nothing happens. Any help, please? Thanks!

\ should be \ " get () , but there are more ideas: i.e. the get function actually needs to be defined out of onload or after any other callback Need to be available globally.You will need a setup like this:

... which is to to to < / Code> Callback, which makes it available for onclick .

In addition, onclic Use standard event binding instead of k :

  dfom.innerHTML = "& lt; Input type = \ "button \" value = \ "hi hi \" & gt; "; dfom.firstChild.addEventListener ('click', get);   



No comments:

Post a Comment