How to wire click event with argument when create input

Q

quoclinh

Hi,

I have the following function that works. It would create an input
that would invoke a click event that call a javascript function:

function createButton(text)
{
var btn = document.createElement('input');
btn.type = 'submit';
btn.value = text;
btn.setAttribute('onclick', 'execAction()'); //this does not
work
btn.setAttribute('class', 'CustomButton'); //this does not
work
btn.onclick = execAction; //this works
return btn;
}

function execAction()
{
alert('in execAction');
return false;
}

The question is, if I have an execAction function with an argument,
how do I change the code in createButton to accomodate it?

Thanks in advance,
Quoc Linh
 
S

Skye Shaw!@#$

Hi,

I have the following function that works. It would create an input
that would invoke a click event that call a javascript function:

function createButton(text)
{
var btn = document.createElement('input');
btn.type = 'submit';
btn.value = text;
btn.setAttribute('onclick', 'execAction()'); //this does not
work
btn.setAttribute('class', 'CustomButton'); //this does not
work
btn.onclick = execAction; //this works
return btn;

}

function execAction()
{
alert('in execAction');
return false;

}

The question is, if I have an execAction function with an argument,
how do I change the code in createButton to accomodate it?


Maybe try:

//....

btn.onclick = execAction;
btn.args = createButtonArgs;
return btn;

function execAction() {

this.args*2.14159
// etc...
}
 
R

RobG

Hi,

I have the following function that works. It would create an input
that would invoke a click event that call a javascript function: [...]
btn.onclick = execAction; //this works [...]

The question is, if I have an execAction function with an argument,
how do I change the code in createButton to accomodate it?

btn.onclick = function() {
execAction(arg1, arg2, ...);
}

Incidentally, if you want a function that fires when a form is
submitted, you are much better off to put it on the form's onsubmit
handler rather than the submit button's onclick handler.
 
S

sylvain.lemasson

btn.className='CustomButton';
btn.onclick=execAction;

Mick

//this does not

You can also create a class to store the argument of your method and
the method itself:

function argumentWrapper(String arg1,String arg2){
this.arg1=arg1;
this.arg2=arg2;

this.exec=execAction;

function execAction(){
arg1*arg2;
//etc..
}
}

wrapper = new argumentWrapper(arg1,arg2);
btn.onClick=wrapper.exec;
 
Q

quoclinh

I have the following function that works. It would create an input
that would invoke a click event that call a javascript function: [...]
btn.onclick = execAction; //this works [...]

The question is, if I have an execAction function with an argument,
how do I change the code in createButton to accomodate it?

btn.onclick = function() {
execAction(arg1, arg2, ...);
}

Incidentally, if you want a function that fires when a form is
submitted, you are much better off to put it on the form's onsubmit
handler rather than the submit button's onclick handler.

Hi all,

Thank you for all your solutions! I really appreciate it. I chose to
go with RobG solution and it's work beautifully. However, being able
to pass the argument value dynamically instead of fixed value would be
tremendously useful as well. I will try other methods when time
allowed.

Thanks again for all your help!

Quoc Linh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top