473,786 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

to pass argument to event handler

My code pass a value of a variable to the event handler's argument by
means of eval statement

here's:

//statements setting evt
for (var i=0; i<evt.periodi.l ength; i++){
//evt.periodi is an array of objs
a=document.crea teElement("a");
a.onclick=eval( 'function (){ cancellaCal(' + evt.periodi[i].id_evt_cal
+ '); };');
a.appendChild(d ocument.createT extNode("cancel la"));
//another statements
}

Without eval trick evt.periodi[i].id_evt_cal is undefined within handler
function.
Is there a better solution?


Thanks
fusillo
Jul 15 '06 #1
2 2079
fusillo <fu*****@nospam .posi-nega.itwrites:
My code pass a value of a variable to the event handler's argument by
means of eval statement

here's:

for (var i=0; i<evt.periodi.l ength; i++){
....
a.onclick=eval( 'function (){ cancellaCal(' + evt.periodi[i].id_evt_cal
+ '); };');
....
Is there a better solution?
Since you need each function value to have different values for "i",
you need a different variable for each. The simplest way to achieve
that is to create a new scope using a function expression and call
it immediately:

a.onclick = (function(local ){
return function(){ cancellaCal(loc al); };
})(evt.periodi[i].id_evt_cal);

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 15 '06 #2
Lasse Reichstein Nielsen ha scritto:
>
a.onclick = (function(local ){
return function(){ cancellaCal(loc al); };
})(evt.periodi[i].id_evt_cal);

/L
your solution is very cool.
thanks for your help.

fusillo


Jul 15 '06 #3

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

Similar topics

4
4561
by: Todd | last post by:
Is it possible to pass an argument by reference through an event in C++ managed code? For example, can I do the following? public __gc class MyClass : public Control { public: __event void MyEvent(bool &Cancel);
3
25289
by: Todd Schinell | last post by:
Back in July, Jeffery Tan posted this: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=OWOTdf0VDHA.2296%40cpmsftngxa06.phx.gbl In response as to how to get click events from a user control to invoke a method in the parent form. This code doesn't seem to work for me, I've tried it a number of times with very simple test cases (A user control with a single button and a parent form with a single text box) and it always...
7
2075
by: liyang3 | last post by:
Hi, I have Class A, B and C. Class A has an instance of B. Class B has an instance of C. In the instance of C, it generates some data that need to be passed back to Class A. But Class C doesnot know anything about Class A. I'm dealing with legacy code. What is the best way to implement it and have the code change to existing classes such as A and B small? Any
2
2496
by: Ronnie Smith | last post by:
I am trying to pass a function (method) address to a user control to associate with an event. The small user control which sends events is contained in a larger user control group which is in turn contained in the main application. I need the event handler to exist at this top level in the application, and so I'm trying to somehow let the small control (two layers down) know the address of this handler, so that the function is called. ...
10
2512
by: Sean Dockery | last post by:
I have the following HTML file that I've been using for testing... <html> <head> <script type="text/javascript"> <!-- function handleWindowLoad() { var items = ; for (var i = 0; i < 11; i++) { items = "item" + (i + 1);
1
6001
by: TarDuk a memory beyond the life | last post by:
I am developing a shopping cart application for jwellary designing \ now i am displaying all the product in the table dynamically. and also show on image button as a add to cart after every item. now when ever client clicks on the add to cart button than i want the item id no in the button's click event's how can be it possible. Here i show the code System.Web.UI.WebControls.ImageButton add = new...
4
10168
by: kadkiran | last post by:
I am facing difficulty when I was trying to pass a string as argument to OnClik Event Handler , Thanks for your suggestions in this regards
6
2077
by: Max | last post by:
i have a event bind function like this(though it is not so robust): bind$=function(o,evt,fn,cb){ var aE='attachEvent'; var aEL='addEventListener'; if(!o&&o){ return o(evt,fn,!!cb); } return o('on'+evt,fn); };
24
55237
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new EventHandler(Onbutton_click); I want to pass more information related that event. & want to use that
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9496
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
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9961
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
8989
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...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.