Javascript: Dynamically Change onclick Event

To dynamically change or add an “onclick” event handler to an element, try:

  • Pingback: » dynamically generated onclick events for dynamically generated elements Shoel's i-log book()

  • Mimi

    YOU ROCK DARKO!!!

  • Athahar

    Hey kevin,

    I think you can try

    mybutton.onclick = new Function(“saveFile(” + xmlDoc + “,” + location + “ )”);

  • Kevin

    Thanks for the code and explanation.

    How can I reference a function and include a parameter.

    I need to include two inputs, xmlDoc and location, for my saveFile function. Without those inputs, my saveFile won’t work.

    var myButton = document.getElementsByName(“saveButton”)[0];
    myButton.onclick = saveFile(xmlDoc,location);

    Thanks,
    Kevin