By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,258 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,258 IT Pros & Developers. It's quick & easy.

Passing event from dynamically created element and event handler?

P: n/a
Adi
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.
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a


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/

Jul 20 '05 #2

P: n/a
Adi
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!
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.