Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Passing event from dynamically created element and event handler?

Reply
Thread Tools

Passing event from dynamically created element and event handler?

 
 
Adi
Guest
Posts: n/a
 
      02-23-2004
Okay, this issue has been annoying me for a little while now.
I need it to work on Mozilla 1.6 & IE 6

This is what I would like to be able to do:

var row = document.createElement("TR");
var tdImage = document.createElement("TD");
var img = document.createElement("IMG");
img.src = "results.gif";
var theA = document.createElement("A");
theA.setAttribute("href", "#");
//Hack which works in both IE6 and Mozilla 1.6
theA["onclick"] = new Function("myFunction(event, 'SomeThing')");
theA.appendChild(img);
tdImage.appendChild(theA);
row.appendChild(tdImage);

Function myFunction(e, theString){
alert(e);
alert(theString);
}

So I am creating a row to be inserted into a table tbody.
The A has a onclick handler.
I want the onclick handler to pass in the event object & some additional
parameters to the function specified.

Running the above gives me "event is not defined" on the:
theA["onclick"] = new Fu... line.

Where as:
<A HREF="#" onclick="myFunction(event, 'SomeThing');">Hello</A>

Works fine!

How do I pass in the elusive event via a dynamically created handler?

Thanks,

Adi.
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      02-23-2004


Adi wrote:

> Okay, this issue has been annoying me for a little while now.
> I need it to work on Mozilla 1.6 & IE 6
>
> This is what I would like to be able to do:
>
> var row = document.createElement("TR");
> var tdImage = document.createElement("TD");
> var img = document.createElement("IMG");
> img.src = "results.gif";
> var theA = document.createElement("A");
> theA.setAttribute("href", "#");
> //Hack which works in both IE6 and Mozilla 1.6
> theA["onclick"] = new Function("myFunction(event, 'SomeThing')");


theA.onclick = function (evt) {
myFunction (evt ? evt : window.event, 'SomeThing');
};


--

Martin Honnen
http://JavaScript.FAQTs.com/

 
Reply With Quote
 
 
 
 
Adi
Guest
Posts: n/a
 
      02-23-2004
On Mon, 23 Feb 2004 14:33:19 +0100, Martin Honnen wrote:

> theA.onclick = function (evt) {
> myFunction (evt ? evt : window.event, 'SomeThing');
> };


Wow, nice one thanks! Works a treat!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Affecting a dynamically created drop down from another dynamically created drop down. msimmons ASP .Net 0 07-16-2009 03:17 PM
Remove child element that was dynamically created BananaQuaalude Javascript 2 12-23-2007 04:58 AM
set dom element class (if element was dynamically created) ted benedict Javascript 3 01-27-2006 01:29 AM
Managing ViewState of a dynamically created Custom Composite Server Control -(where the original is also dynamically created) dickster ASP .Net Building Controls 0 12-08-2005 09:32 AM
Passing webcontrols created dynamically on user control placed on a placeholder in a form to another page elkay ASP .Net Web Controls 5 12-22-2004 05:31 PM



Advertisments