Passing event from dynamically created element and event handler?

Discussion in 'Javascript' started by Adi, Feb 23, 2004.

  1. Adi

    Adi Guest

    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.
     
    Adi, Feb 23, 2004
    #1
    1. Advertising

  2. Adi wrote:

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


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Feb 23, 2004
    #2
    1. Advertising

  3. Adi

    Adi Guest

    On Mon, 23 Feb 2004 14:33:19 +0100, Martin Honnen wrote:

    > theA.onclick = function (evt) {
    > myFunction (evt ? evt : window.event, 'SomeThing');
    > };


    Wow, nice one thanks! Works a treat!
     
    Adi, Feb 23, 2004
    #3
    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. =?Utf-8?B?QW5kcmV3IFdpZWQ=?=

    Adding event handler for dynamically created controls

    =?Utf-8?B?QW5kcmV3IFdpZWQ=?=, Apr 19, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,413
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Apr 25, 2004
  2. Replies:
    1
    Views:
    734
    Damien
    Feb 22, 2007
  3. gnewsgroup
    Replies:
    4
    Views:
    693
    gnewsgroup
    Feb 19, 2008
  4. msimmons
    Replies:
    0
    Views:
    525
    msimmons
    Jul 16, 2009
  5. ted benedict
    Replies:
    3
    Views:
    156
Loading...

Share This Page