assigning stuff to onclick, via javascript

Discussion in 'Javascript' started by yawnmoth, Oct 20, 2006.

  1. yawnmoth

    yawnmoth Guest

    Say I wanted to assign something to this.onclick. I could do this by
    doing "this.onclick = Function('...')", but I'm not sure if that's the
    prefered way to do it, per the official W3C specs?
     
    yawnmoth, Oct 20, 2006
    #1
    1. Advertising

  2. yawnmoth wrote:
    > Say I wanted to assign something to this.onclick. I could do this by
    > doing "this.onclick = Function('...')", but I'm not sure if that's the
    > prefered way to do it, per the official W3C specs?


    Is there any official W3C spec that defines an onclick property on HTML
    or XML element nodes? I don't think so.
    The HTML 4 specification defines event handler attributes like onclick
    but there is nothing offical currently saying that such attributes are
    reflected in the HTML DOM as properties. Browser implementations however
    have always done so and exposed such an event handler as a function
    object. So doing
    this.onclick = function (evt) { /* function body here */ };
    or
    this.onclick = new Function ("evt", "//function body here");
    where this is a HTML element object is fine.

    The W3C DOM Level 2 Events module however defines addEventListener so
    following that (where supported, IE 5/6/7 does not) you can do e.g.

    function aClickHandler (evt) { /* function body here */ }

    this.addEventListener(
    'click',
    aClickHandler,
    false
    );

    to set and

    this.removeEventListener(
    'click',
    aClickHandler,
    false
    );

    to remove.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Oct 20, 2006
    #2
    1. Advertising

  3. yawnmoth wrote:
    > Say I wanted to assign something to this.onclick. I could do this by
    > doing "this.onclick = Function('...')", but I'm not sure if that's the
    > prefered way to do it, per the official W3C specs?
    >


    Not all browsers conform to teh W3C specs. To do it in a cross-browser way, Use the YAHOO Event object from the YAHOO UI: http://developer.yahoo.com/yui/

    And then use

    YAHOO.util.Event.addListener(myDomElement, "click", myFunction, scopeObject, true);

    That will attach the function myFunction, and call it in the scope of scopeObject.

    Nige
     
    TheBagbournes, Oct 21, 2006
    #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. RobG
    Replies:
    4
    Views:
    127
  2. weston
    Replies:
    1
    Views:
    253
    Richard Cornford
    Sep 22, 2006
  3. --[zainy]--
    Replies:
    1
    Views:
    288
    OmegaJunior
    Mar 31, 2007
  4. yawnmoth
    Replies:
    17
    Views:
    164
    dhtml
    Oct 2, 2008
  5. duckkiller53

    problem assigning function to <div>.onclick

    duckkiller53, Apr 21, 2011, in forum: Javascript
    Replies:
    4
    Views:
    188
Loading...

Share This Page