Passing event as parameter to dynamic function

Discussion in 'Javascript' started by Ron, Nov 13, 2008.

  1. Ron

    Ron Guest

    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
     
    Ron, Nov 13, 2008
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Nov 13, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    5
    Views:
    630
    John Carson
    Jul 30, 2003
  2. Kyzer
    Replies:
    2
    Views:
    774
    Duane Hebert
    May 6, 2005
  3. Azdo
    Replies:
    2
    Views:
    442
  4. Mister B
    Replies:
    8
    Views:
    570
    Nick Keighley
    Aug 26, 2010
  5. AzamSharp
    Replies:
    2
    Views:
    187
Loading...

Share This Page