473,379 Members | 1,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

How to wire click event with argument when create input

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

Jun 22 '07 #1
4 2779
On Jun 22, 12:10 am, quoclinh <lequocl...@yahoo.comwrote:
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...
}

Jun 22 '07 #2
On Jun 22, 5:10 pm, quoclinh <lequocl...@yahoo.comwrote:
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.
--
Rob

Jun 22 '07 #3
Skye Shaw!@#$ wrote:
On Jun 22, 12:10 am, quoclinh <lequocl...@yahoo.comwrote:
>>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');
btn.className='CustomButton';
btn.onclick=execAction;

Mick
//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...
}
Jun 22 '07 #4
On Jun 22, 1:56 am, RobG <r...@iinet.net.auwrote:
On Jun 22, 5:10 pm, quoclinh <lequocl...@yahoo.comwrote:
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.

--
Rob
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

Jun 26 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Csaba2000 | last post by:
I'd like to be able to simulate a mouse click on an arbitrary HTML element (for the purpose of invoking whatever behaviour that element would have, had a real mouse click come to it) so that my...
4
by: Csaba2000 | last post by:
I want to be able to programatically click on the center of an <INPUT type=image ...> element (I only care about IE 5.5+). This should work regardless of whether IE has focus. Normally you would...
8
by: George Hester | last post by:
In a page I have when the user left-clicks the page a Input box for a form gets the focus. But if the user right-clicks the page the Input box is not getting the focus. I'd like the Input box to...
7
by: MLH | last post by:
I tried the following code to prevent a checkbox from being updated (going from a value of Null to True, from True to False or from False to True). I was surprised it did not work. Can anyone...
2
by: Uninvisible | last post by:
I have put together a db for a law firm to keep track of counterfeit activities. There are four parent tables: tblContact tblTransaction tblAction tblFile I have created a form,...
2
by: Linda | last post by:
Hi, How do I dynamically add linkbuttons and wire them to same event? I am able to add linkbuttons but they do not fire the event. Does anybody have a working sample? Many thanks, Linda
3
by: CodeRazor | last post by:
I am creating an aspx page using C# and would like to be able to dynamically create linkbuttons that all run the same fuction on the click event. However, I would like the function to accept a...
0
by: Demetri | last post by:
I have created a web control that can be rendered as either a linkbutton or a button. It is a ConfirmButton control that allows a developer to force a user to confirm if they intended to click it...
2
by: Chu | last post by:
Thanks everyone for taking a moment to read this. I've got a page where I use a LinkButton and I wire up a dynamic event to the button. When the user clicks the button, the event is fired as...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.