473,666 Members | 2,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic onclick event

Is there any way I can attach a function dynamically which takes some
parameters ?

<html>
<head>
<script>
doSomething = function(x)
{
alert(x);
}

reattach = function()
{
// doesn't work
document.getEle mentById("btnOn e").onclick = doSomething(200 );

// neither does this
document.getEle mentById("btnTw o").addEventLis tener("onclick" ,
doSomething(200 ), true);
}
</script>
</head>
<body>
<form>
<input type="button" name="btnOne" id="btnOne"
onclick="doSome thing(2)" value="One" />
<br />
<br />
<input type="button" name="btnTwo" id="btnTwo" onclick="reatta ch()"
value="Dynamic" />
</form>
</body>
</html>

By using the first syntax I just end up executing the function.

By using the addEventListene r function I get some weird error like:
"uncaught exception: [Exception... "Could not convert JavaScript
argument" nsresult: "0x80570009 (NS_ERROR_XPC_B AD_CONVERT_JS)"
location: "JS frame ::"

Any help is greatly appreciated.

May 13 '07 #1
4 14824
ASM
ge************* *@gmail.com a écrit :
Is there any way I can attach a function dynamically which takes some
parameters ?

<html>
<head>
<script>
doSomething = function(x)
{
alert(x);
}

reattach = function()
{
// doesn't work
document.getEle mentById("btnOn e").onclick = doSomething(200 );
Perhaps :
document.getEle mentById("btnOn e").onclick = 'doSomething(20 0)';

Certainly :
document.getEle mentById("btnOn e").onclick = function() {
doSomething(200 );
}

or :
document.getEle mentById("btnOn e").onclick=Fun ction('doSometh ing(200)');
May be :
document.getEle mentById("btnTw o").addEventLis tener("onclick" ,
"doSomething(20 0)",
true);
}
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
May 13 '07 #2
Perhaps :
document.getEle mentById("btnOn e").onclick = 'doSomething(20 0)';

Certainly :
document.getEle mentById("btnOn e").onclick = function() {
doSomething(200 );
}

or :
document.getEle mentById("btnOn e").onclick=Fun ction('doSometh ing(200)');

May be :
document.getEle mentById("btnTw o").addEventLis tener("onclick" ,
"doSomething(20 0)",
true);

}
What if a variable is used inside the function instead of a literal ?
Like doSomething(myV ar) ? I guues I am facing some scoping problems
when using your second approach.

var i = 100;
document.getEle mentById("btnOn e").onclick = function()
{ doSomething(i); }

For some weird reason, it can't read the value of i inside the
function scope and it always shows blank. Then I tried using:

var i = 100;
document.getEle mentById("btnOn e").onclick = function(i)
{ doSomething(i); }

The above approach always gives 'i' the value of 'document.mouse event'
for no good reason.

And the attachEventList ener method as usual gives me this error:
"uncaught exception: [Exception... "Could not convert JavaScript
argument" nsresult: "0x80570009 (NS_ERROR_XPC_B AD_CONVERT_JS)"
location: "JS frame ::"

Any suggestions?
May 13 '07 #3
On May 14, 5:31 am, getsanjay.sha.. .@gmail.com wrote:
Perhaps :
document.getEle mentById("btnOn e").onclick = 'doSomething(20 0)';
Certainly :
document.getEle mentById("btnOn e").onclick = function() {
doSomething(200 );
}
or :
document.getEle mentById("btnOn e").onclick=Fun ction('doSometh ing(200)');
May be :
document.getEle mentById("btnTw o").addEventLis tener("onclick" ,
"doSomething(20 0)",
true);
}

What if a variable is used inside the function instead of a literal ?
Like doSomething(myV ar) ? I guues I am facing some scoping problems
when using your second approach.

var i = 100;
document.getEle mentById("btnOn e").onclick = function()
{ doSomething(i); }

For some weird reason, it can't read the value of i inside the
function scope and it always shows blank. Then I tried using:

var i = 100;
document.getEle mentById("btnOn e").onclick = function(i)
{ doSomething(i); }

The above approach always gives 'i' the value of 'document.mouse event'
for no good reason.

And the attachEventList ener method as usual gives me this error:
"uncaught exception: [Exception... "Could not convert JavaScript
argument" nsresult: "0x80570009 (NS_ERROR_XPC_B AD_CONVERT_JS)"
location: "JS frame ::"

Any suggestions?
You have discovered a number of things in regard to events and
closures. A good introductoin to events is at Quirskmode, and to
closures in the FAQ notes (see links below):

Quirksmode: Introduction to events (there are many pages, read them
all):
<URL: http://www.quirksmode.org/js/introevents.html >

Closures (rather long and detailed but it's all good stuff):
<URL: http://www.jibbering.com/faq/faq_notes/closures.html >
--
Rob

May 13 '07 #4
ASM
ge************* *@gmail.com a écrit :
>
What if a variable is used inside the function instead of a literal ?
Like doSomething(myV ar) ? I guues I am facing some scoping problems
when using your second approach.

var i = 100;
document.getEle mentById("btnOn e").onclick = function()
{ doSomething(i); }
Any suggestions?

var i = 200;
document.getEle mentById("btnOn e").onclick=Fun ction('doSometh ing("'+i+'")');
<html>
<script type="text/javascript">
function $(id) { return document.getEle mentById(id); }
function addOnClick(){
for(var i=1; i<=5; i++) {
$('field'+i).on click = Function('alarm ("field'+i+'")' );
$('field'+i).st yle.cursor = 'pointer';
}
}
function alarm(txt) {
alert('Yes it is the field :\n\t\t\t'+txt) ;
alert('Verifica tion : '+$(txt).id);
}
onload = addOnClick;
</script>
<p id="field1"clic k 1 </p>
<p id="field2"clic k 2 </p>
<p id="field3"clic k 3 </p>
<p id="field4"clic k 4 </p>
<p id="field5"clic k 5 </p>
</html>

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
May 13 '07 #5

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

Similar topics

4
39449
by: Dipin | last post by:
Hi All; I have this javascript which is adding a new button to the column in the row which is created dynamically, the innerhtml shows that the onclick event is correctly added but it never gets invoked when I click it. newcol = doc.createElement("TD"); newbutton = doc.createElement("input"); newbutton.name = "newChange"+__uid;
2
7020
by: Andreas Knollmann | last post by:
Hi, I create an object like this: var cell = document.createElement("td"). It doesn't have to be cell. I want this cell to use the onclick event. What doesn't work in the IE as well as with Mozilla is: cell.onclick = "whatever()";
17
4870
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick event ? I've tried something like this: oncl = document.getElementById('mySpan').onclick oncl = oncl + '\n;alert(\'added\')' document.getElementById('mySpan').onclick = oncl
5
2577
by: moondaddy | last post by:
I have a <a> element in a datagrid which wraps some asp.net labels. this element also has an onclick event which does not fire in netscape 6 (and perhaps other browsers for all I know...). Below is the code for this. the onclick event calls a javascript function which I put an alert in the firt line to tell me if its working. It does work in IE. Any ideas on how to get netcrap... oops, I'm sorry, netscape to fire the onclick event? ...
5
19657
by: RA | last post by:
I have created a button dynamically; which has been added to a TableCell of a TableRow of a Table control. Is there a way to add onclick event which calls a procedure on the Server-side itself. Any suggestions? I tried with btnAdd.Attributes.Add("onClick","AddItem();")
5
13945
by: Stuart Shay | last post by:
Hello All I am working on ASP.NET 1.1 Custom Pager that allows a User to Enter a Number in a TextBox and go to the page selected. Since the OnClick Event does not work in ASP.NET 1.1 for a TextBox I want to use a hidden button to fire when the Onclick Event is fired for the TextBox.
2
2684
by: Pola Bhaskar | last post by:
Hi, I would like to override the onclick event of an element by this. I will be passing the javascript method to be set. function setOnClickEvent(control, method){ document.getElementById(control).onclick = function(){method}; } ---------------------------------------------------------------------
3
1754
by: naurus | last post by:
I have some code that must change the onclick function of a DIV: function navChange(id,auto){ check = fetchById(id + "More").style.display; if(check == "block"){ showLess(id,auto); } else{ showMore(id,auto); } }
6
1745
by: theS70RM | last post by:
Hi Guys, Im trying to create lots of divs all with there own onclick event... Heres the code: <html> <head> <script language="javascript">
6
15465
by: tomaz | last post by:
<SCRIPT LANGUAGE=javascript> function tdclick (ObjTD) { ArrLinks = ObjTD.getElementsByTagName ("A"); document.getElementById("link").onclick(); } </SCRIPT>
0
8352
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7378
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6189
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2005
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.