Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Passing event as parameter to dynamic function

Reply
Thread Tools

Passing event as parameter to dynamic function

 
 
Ron
Guest
Posts: n/a
 
      11-13-2008
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
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      11-13-2008
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/
 
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
Passing parameter to function not expecting parameter Mister B C Programming 8 08-26-2010 08:01 AM
How to pass a parameter for a function parameter in a function AzamSharp Javascript 2 07-05-2008 12:24 AM
Passing in a OnMouseDown/OnMouseUp event handler as a parameter of a function Kyzer C++ 2 05-06-2005 12:53 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments