setting events

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wim Roffal

    #1

    setting events

    I have an object

    <button id=summer>

    Now I want to dynamically set an event for this object:

    function foo()
    {
    }
    myobject = document.getEle mentById("summe r");
    myobject.onClic k = foo;

    Problem is that I want to give the function an argument. "foo(this)" instead
    of just "foo".

    How can I achieve that?

    Thanks,

    Wim


  • Lasse Reichstein Nielsen

    #2
    Re: setting events

    "Wim Roffal" <wimroffelpleas e@nospamm-planet.nl> writes:
    [color=blue]
    > <button id=summer>[/color]
    [color=blue]
    > Now I want to dynamically set an event for this object:[/color]
    ....[color=blue]
    > myobject = document.getEle mentById("summe r");
    > myobject.onClic k = foo;
    >
    > Problem is that I want to give the function an argument. "foo(this)" instead
    > of just "foo".[/color]

    ---
    var self = this; // save reference in variable
    myobject.onclic k = function(){ foo(self); };
    ---
    Notice:
    "onclick" must be all lower case.
    The function *is* given one argument: the event. Here it is discarded.
    (except in IE, where the event is a global variable instead)

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Mike Foster

      #3
      Re: setting events

      Wim Roffal wrote:
      [color=blue]
      > I have an object
      >
      > <button id=summer>
      >
      > Now I want to dynamically set an event for this object:
      >
      > function foo()
      > {
      > }
      > myobject = document.getEle mentById("summe r");
      > myobject.onClic k = foo;
      >
      > Problem is that I want to give the function an argument. "foo(this)" instead
      > of just "foo".
      >
      > How can I achieve that?
      >[/color]


      The Event object is the only argument passed to a listener.
      However, there are ways to accomplish what I think you want.
      Have a look at this demo:

      Comment

      Working...