473,626 Members | 3,974 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding dynamic events to objects in IE!?

Hi,

Okay, here's my problem...

I'm dynamically building some content (similar to this):

var fooObj = document.create Element('div');
fooObj.setAttri bute('id', 'uniqueNumber') ;

etc.

The issue I have, is that I want to do (similar to this):

fooObj.setAttri bute('onclick', 'func1();func2( ' + some.dyn.refere nce +
');return false;');
Of course, it works like butta, in *Mozilla*, and *Opera*... but _IE_
just quietly ignores it...

-------------------

Now, I know I can use:

fooObj.click = nameOfFuncWithN oQuotesOrBracke ts;

but, I'm limited to 1 (one) function, and *more* importantly, I can't
pass a parameter to the function... (IIRC)

-------------------

Looking for ideas... ;-)

Please keep in mind though...

a.) I *have* to dynamically create this element (it doesn't exist until
run-time)
b.) I *have* to be able to pass parameter(s)
c.) *Ideally* I would like to be able to call several consecutive
statements (e.g. any combo of js stmts / functions / returns), however
if I can call one function with parameters, I *suppose* I can make that
function in turn call the one(s) I need.

d.) Allthough I would really like this to work in IE too, if it turns
out that it can't, well, that's okay too... This would likely be the
'official' last straw in supporting this Browser for this mini-app.
(read: Debugging in IE is like shaving with a chainsaw... you *could*
do it, but why even try!)

Cheers,
Steve

Sep 7 '05 #1
10 6551
st************* *@gmail.com wrote:
Hi,

Okay, here's my problem...

I'm dynamically building some content (similar to this):

var fooObj = document.create Element('div');
fooObj.setAttri bute('id', 'uniqueNumber') ;

I'm pretty sure if you search this newsgroup you'll find a solution to
that, but see below.
etc.

The issue I have, is that I want to do (similar to this):

fooObj.setAttri bute('onclick', 'func1();func2( ' + some.dyn.refere nce +
');return false;');
fooObj.onclick = function() {
// statements using parameters
};

This creates an anonymous function, be careful with closures[1]. You
can also create a function that adds the onclick and you pass your
parameters to it:

var fooObj = document.create Element('div');
addOnclick(fooO bj, parm1, parm2);
...
}

function addOnclick(obj, p1, p2)
{
obj.onclick = function() {
// Statements using p1 and p2
}
}

The second method may solve some closure issues.

In the first method, the parameters are evaluated when the onclick runs
which is handy if you want them to be sort of like globals - their value
may change other than through the onlclick. In the second method,
parameters are passed to addOnclick and evaluated when added to the
element so they should not change.

1. See the group FAQ notes:

<URL:http://jibbering.com/faq/faq_notes/closures.html>

2. Here are some threads that may be of interest:

<URL:http://groups.google.c o.uk/group/comp.lang.javas cript/browse_frm/thread/fbf6b4f6c9dc180 6/96b2393ad65dd1a a?q=dynamically +add+onclick&rn um=9&hl=en#96b2 393ad65dd1aa>
<URL:http://groups.google.c o.uk/group/comp.lang.javas cript/browse_frm/thread/b58ee83c1510349 7/bcbd2eaa0ffa3c1 d?q=dynamically +add+onclick&rn um=13&hl=en#bcb d2eaa0ffa3c1d>
[...] Please keep in mind though...

a.) I *have* to dynamically create this element (it doesn't exist until
run-time)
Not a problem
b.) I *have* to be able to pass parameter(s)
Not a problem
c.) *Ideally* I would like to be able to call several consecutive
statements (e.g. any combo of js stmts / functions / returns), however
if I can call one function with parameters, I *suppose* I can make that
function in turn call the one(s) I need.
You can add multiple functions if you like, with lots of parameters

d.) Allthough I would really like this to work in IE too, if it turns
out that it can't, well, that's okay too... This would likely be the
'official' last straw in supporting this Browser for this mini-app.
(read: Debugging in IE is like shaving with a chainsaw... you *could*
do it, but why even try!)
The above will work in IE too. There are reasonable debugging solutions
for IE, some require MS Office or Visual Studio to get them (search the
newsgroup, there's a recent series of posts regarding debugging in IE).
Here's one:

<URL:http://groups.google.c o.uk/group/comp.lang.javas cript/browse_frm/thread/4e1ef68ed8443af f/8c4dd4a80dafb83 2?q=IE+debuggin g+&rnum=13&hl=e n#8c4dd4a80dafb 832>

Cheers,
Steve

--
Rob
Sep 7 '05 #2
RobG wrote:
var fooObj = document.create Element('div');
addOnclick(fooO bj, parm1, parm2);
...
}

function addOnclick(obj, p1, p2)
{
obj.onclick = function() {
// Statements using p1 and p2
}
}

The second method may solve some closure issues.


Doesn't this create a memory leak in IE too?
The DOM Node with the event handler is now in the same scope, creating a
circular reference. I thought.....
That's why I always do something like:

var fooObj = document.create Element('div');
fooObj.onclick = addOnclick(parm 1, parm2);
...
}

function addOnclick(p1, p2)
{
return = function() {
// Statements using p1 and p2
}
}

Robert.
Sep 8 '05 #3
I've heard a lot about this "circular reference" with the DOM...can you
more clearly explain this scope / mem leak issue?

-Brendan

Sep 9 '05 #4
Donius wrote:
I've heard a lot about this "circular reference" with the DOM...can you
more clearly explain this scope / mem leak issue?

-Brendan


The first link in my post above:

<URL:http://jibbering.com/faq/faq_notes/closures.html>

--
Rob
Sep 11 '05 #5
RobG wrote:
Donius wrote:
I've heard a lot about this "circular reference" with the DOM...can you
more clearly explain this scope / mem leak issue?

-Brendan


The first link in my post above:

<URL:http://jibbering.com/faq/faq_notes/closures.html>


Would you like to respond to my question regarding your example and
whether that does or does not create a memory leak?
Your example was:

var fooObj = document.create Element('div');
addOnclick(fooO bj, parm1, parm2);
...
}

function addOnclick(obj, p1, p2)
{
obj.onclick = function() {
// Statements using p1 and p2
}
}
The DOM Node with the event handler is now in the same scope, creating a
circular reference. I thought.....

Sep 12 '05 #6
On 12/09/2005 15:12, Robert wrote:

[snip]
function addOnclick(obj, p1, p2)
{
obj.onclick = function() {
// Statements using p1 and p2
}
}

The DOM Node with the event handler is now in the same scope,
creating a circular reference.


Yes, but easily fixed by assigning null to the obj argument after the
listener has been added. If the listener needs to refer to that element,
it can use the this operator.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Sep 13 '05 #7
Michael Winter wrote:
On 12/09/2005 15:12, Robert wrote:

[snip]
function addOnclick(obj, p1, p2)
{
obj.onclick = function() {
// Statements using p1 and p2
}
}

The DOM Node with the event handler is now in the same scope,
creating a circular reference.

Yes, but easily fixed by assigning null to the obj argument after the
listener has been added. If the listener needs to refer to that element,
it can use the this operator.

Like this?
function addOnclick(obj, p1, p2){
obj.onclick = function() {
// Statements using p1 and p2
obj=null;
}
}

Puzzled.
Mick
Sep 13 '05 #8
On 13/09/2005 15:00, Mick White wrote:
Michael Winter wrote:
[...] assigning null to the obj argument after the listener has
been added. [...]


Like this?
function addOnclick(obj, p1, p2){
obj.onclick = function() {
// Statements using p1 and p2
obj=null;
}
}


No.

function addOnclick(obj, p1, p2) {
obj.onclick = function() {
/* ... */
};
obj = null;
}

After the listener has been added. :)

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Sep 13 '05 #9
Michael Winter wrote:
On 13/09/2005 15:00, Mick White wrote:
Michael Winter wrote:
[...] assigning null to the obj argument after the listener has been
added. [...]

Like this?
function addOnclick(obj, p1, p2){
obj.onclick = function() {
// Statements using p1 and p2
obj=null;
}
}

No.

function addOnclick(obj, p1, p2) {
obj.onclick = function() {
/* ... */
};
obj = null;
}

After the listener has been added. :)

OK, I think I have it now.
Thanks.
Mick
Sep 14 '05 #10

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

Similar topics

5
1634
by: Patrick | last post by:
Hello I have the following problem. I have an aspx Page. This page contains an ASP-Table Object. So in that table, I have a linkbutton. So, I can't edit the event of that button trough the WYSIWYG editor, so I co to the CodeBehind and add the Event in the InitializeComponent() Method like this this.lbTest.Click += new System.EventHandler(this.lbTest_Click); so far, all works fine. now if i go to the WYSIWYG editor, add another...
2
2105
by: Tim Marsden | last post by:
Hi, This is what I am doing, please comment if this is the correct way. I need to add controls to a form dynamically. Within the Page_Load event (is not Postback) I run a routine to create the controls, and populate the dropdowns etc, I use addhandler to define delegates to capture events raised by the controls. I store these controls in a hashtable.
2
2225
by: JezB | last post by:
I'm adding WebControl objects to a Page dynamically on Page_Load, but I'm having trouble attaching events to these. For example, adding an image button :- ImageButton nb = new ImageButton(); nb.ImageUrl = "text.gif"; nb.ToolTip = "Edit Text"; nb.Click += new ImageClickEventHandler(b1_Click); myPlaceholder.Controls.Add(nb);
2
1354
by: Jörn von Holten | last post by:
Hi, my 3-tier system has a separate bizz-logic and storage service (MVC paradigm as usual) and the bizz-logic layer shall drive the logic (workflow) of the ASP site. My "idea" is (alike Xaml but I am not allowed to use Xaml now) to store meta info defining the design of the dialogs. These are "rendered" to ASP.NET 2.0 objects in the visualization layer (code-behind). Obviously I can move to Xaml quite easily later on :-)
1
2026
by: seanmayhew | last post by:
I have a form page that that while editing saves the data to an xml doc before submitting to db. On each page unload it saves the xmldoc as the user can add multiple items to the company like product types etc. So for instance Im adding a fruit company while adding a fruit company I allow the user to add types of fruit they carry and display it dynamically using an <asp:table> with image
0
2065
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab, System and Software Engineering Lab), ULB (deComp) and the Belgian Association for Dynamic Languages (BADL) are very pleased to invite you to a whole day of presentations about the programming languages Self, Smalltalk and Common Lisp by experts in...
5
2718
by: Steve Moreno | last post by:
Hi all, I've got a web form that I've written code to create an array of DropDownList controls on the page depending on how many records are pulled back. The code to create the controls is working fine but now I need to add events to the newly created DropDownList controls. I need to add the SelectedIndexChanged event and I'm having a hard time getting the code to add events to the controls since the names (IDs) are varied to include...
1
6061
by: Kenneth Siewers Møller | last post by:
Hi there I have a question about dynamic event binding. I have a class called Fraction in which a number of events are declared. Some of the events are: OnNameChanged OnCostChanged OnDateChanged
4
2202
by: Mike Lowery | last post by:
I have an ASP.Net page that simply generates a dynamic page using Response.Write() statements to generate the HTML. This works great except that one of the things I want to generate is a LinkButton. I know this can be done by creating a Panel and adding it using Panel.Controls.Add(LinkButton) but the problem is there are no controls on the page--I want to dynamically add everything using simple Response.Write statements. But it doesn't...
7
2902
by: Christopher Pisz | last post by:
My problem is my derived class is getting called twice instead of the base and then the derived. I thought this was the purpose for virtuals and dynamic casting :/ I want my base class to have its method called and then the derived class have its method called. What am I not understanding? Int the following code, my Event Tester class is getting called twice for keyboard events when I step through the debugger:...
0
8266
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
8199
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
8705
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...
0
8638
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
8505
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
7196
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
4092
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...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1811
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.