Passing event from dynamically created element and event handler?

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.
 
M

Martin Honnen

Adi said:
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');
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top