Passing event as parameter to dynamic function

R

Ron

Hi,

I'm using javascript to dynamically generate a table (ajax/xml).
What I want to achieve is when a table cell is clicked it needs to
call a function and pass the event as a parameter (In IE I can use the
window.event to access the event once the function is called, but in
Firefox it needs to be passed as a parameter.)

In normal html it would look like this:

<td onclick="javascript:fnSetSub(event)"></td>

to do this using javascript I've got something that looks like this:

var newTr = document.createElement('TR');
var newTd = document.createElement('TD');
newTd.onclick = function() {fnSetSub (event);};
newTr.appendChild(newTd);

but when the cell is clicked it comes up with an "e is undefined"
error in Firefox. I've tried using this.event, newTd.event but
nothing works. The function called looks something like this:

function fnSetSub (e) {
var targ;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
}

Any help will be much appreciated.
Ron
 
M

Martin Honnen

Ron said:
var newTd = document.createElement('TD');
newTd.onclick = function() {fnSetSub (event);};

You need
newTd.onclick = function (evt) { fnSetSub(evt); };
or
newTd.onclick = fnSetSub;
meaning you need to assign a function that as its first argument takes
an event parameter so that the browser can pass an event object in when
it calls the function.
 

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

No members online now.

Forum statistics

Threads
473,836
Messages
2,569,748
Members
45,545
Latest member
rapter____0

Latest Threads

Top