Claudio Brasser Claudio Brasser - 1 year ago 110
HTML Question

Javascript add function call with parameters to button

I would like to add a function call including parameters to a HTML button via javascript. I know there are plenty of questions to this subject but none of the answers i found did work for me.

Note: My HTML and JS are in separate files which are correctly linked (the JS code works)

I can add the function call like this:

var $add = $("#add");

does not work.(Did also try with specific integer)

I have also tried it the following way:

document.getElementById('add').onclick = function() {myFunction(i);};

But like that the function does not even get applied to the button.

My function is defined in the JS file like this:

function myFunction(length) {

//do stuff with length I would notice


Answer Source

You can use some thing like function bind or do it using handler:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download