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

Passing event as parameter to dynamic function

P: n/a
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
Nov 13 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Ron wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 13 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.