The mothod "on" not available for a button?!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • K Viltersten

    The mothod "on" not available for a button?!

    It's perhaps something elementary. I tried
    to do suff as follows.

    document.getEle mentById ('Button1').on (
    'click', funcion () {});

    For some reason, the method isn't there. According
    to FireBug i get this.

    document.getEle mentById ('Button1').on
    (gives nothing - no such method)

    while

    Ext.get ('Button1').on
    (gives function ())

    What do i miss?

    --
    Regards
    Konrad Viltersten
  • Robin Rattay

    #2
    Re: The mothod "on&quo t; not available for a button?!

    On 23 Mai, 12:08, "K Viltersten" <t...@vilterste n.comwrote:
    It's perhaps something elementary. I tried
    to do suff as follows.
    >
    document.getEle mentById ('Button1').on (
    'click', funcion () {});
    >
    For some reason, the method isn't there. According
    to FireBug i get this.
    Because there isn't such a method.
    document.getEle mentById ('Button1').on
    (gives nothing - no such method)
    Right. document.getEle mentById returns a DOM reference, and the DOM
    API doesn't have a method "on".
    Ext.get ('Button1').on
    (gives function ())
    Ext.get is a method of a 3rd party libary (I'm guessing "Ext" :-) that
    returns something else than a DOM reference which does have an "on"
    method.

    I'd suggest as a JavaScript beginner, you should avoid using 3rd party
    libraries until you know what you're doing, or at least read their
    documentation.

    With the DOM API events are set either directly:

    document.getEle mentById('Butto n1').onclick = function() { ... }

    or with addEventListene r:

    document.getEle mentById('Butto n1').addEventLi stener("click",
    function() { ... }, true);

    Which IE however doesn't support. You need to use attachEvent for IE.
    More details at: http://developer.mozilla.org/en/docs...dEventListener

    Robin

    Comment

    • Henry

      #3
      Re: The mothod &quot;on&quo t; not available for a button?!

      On May 23, 11:08 am, K Viltersten wrote:
      It's perhaps something elementary. I tried
      to do suff as follows.
      >
      document.getEle mentById ('Button1').on (
      'click', funcion () {});
      >
      For some reason, the method isn't there. According
      to FireBug i get this.
      >
      document.getEle mentById ('Button1').on
      (gives nothing - no such method)
      >
      while
      >
      Ext.get ('Button1').on
      (gives function ())
      >
      What do i miss?
      That - document.getEle mentById - returns an element from the DOM and -
      Ext.get -(whatever that is) either returns a different sort of object
      or returns a DOM element that has been subject to (direct or indirect)
      augmentation prior to being returned.

      Neither the W3C DOM specifications nor historical practice suggest
      that an element should be expected to have an - on - method.

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: The mothod &quot;on&quo t; not available for a button?!

        Robin Rattay wrote:
        With the DOM API events are set either directly:
        >
        document.getEle mentById('Butto n1').onclick = function() { ... }
        >
        or with addEventListene r:
        >
        document.getEle mentById('Butto n1').addEventLi stener("click",
        function() { ... }, true);
        Reference Worms[tm] are error-prone, and should therefore be avoided.

        // add feature tests here

        var o = document.getEle mentById('Butto n1');
        if (o)
        {
        // add feature tests here

        o.addEventListe ner("click", function() { ... }, true)
        }

        Adding a capturing event listener (`true') through the standards-compliant
        addEventListene r() method of the EventTarget interface is _not_ equivalent
        to assigning to the proprietary event handler property (`on...').
        Therefore, the third argument should be `false' unless compatibility with
        non-DOM2 UAs is not an issue (seldom).
        Which IE however doesn't support. You need to use attachEvent for IE.
        However, IE/MSHTML does support the proprietary event handler property,
        which should to be used instead of attachEvent():




        PointedEars
        --
        Anyone who slaps a 'this page is best viewed with Browser X' label on
        a Web page appears to be yearning for the bad old days, before the Web,
        when you had very little chance of reading a document written on another
        computer, another word processor, or another network. -- Tim Berners-Lee

        Comment

        • K Viltersten

          #5
          Re: The mothod &quot;on&quo t; not available for a button?!

          >It's perhaps something elementary. I tried
          >to do suff as follows.
          >>
          >document.getEl ementById ('Button1').on (
          > 'click', funcion () {});
          >>
          >For some reason, the method isn't there. According
          >to FireBug i get this.
          >
          Because there isn't such a method.
          >
          >document.getEl ementById ('Button1').on
          >(gives nothing - no such method)
          >
          Right. document.getEle mentById returns a DOM reference, and the DOM
          API doesn't have a method "on".
          >
          >Ext.get ('Button1').on
          >(gives function ())
          >
          Ext.get is a method of a 3rd party libary (I'm guessing "Ext" :-) that
          returns something else than a DOM reference which does have an "on"
          method.
          >
          I'd suggest as a JavaScript beginner, you should avoid using 3rd party
          libraries until you know what you're doing, or at least read their
          documentation.
          >
          With the DOM API events are set either directly:
          >
          document.getEle mentById('Butto n1').onclick = function() { ... }
          >
          or with addEventListene r:
          >
          document.getEle mentById('Butto n1').addEventLi stener("click",
          function() { ... }, true);
          >
          Which IE however doesn't support. You need to use attachEvent for IE.
          More details at:
          http://developer.mozilla.org/en/docs...dEventListener
          Thanks for the head up regarding IE. Also, thanks for the
          very insightful reply.

          --
          Regards
          Konrad Viltersten

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: The mothod &quot;on&quo t; not available for a button?!

            Henry wrote:
            On May 23, 11:08 am, K Viltersten wrote:
            >document.getEl ementById ('Button1').on
            >(gives nothing - no such method)
            >>
            >while
            >>
            >Ext.get ('Button1').on
            >(gives function ())
            >>
            >What do i miss?
            >
            That - document.getEle mentById - returns an element from the DOM and -
            Ext.get -(whatever that is) either returns a different sort of object
            or returns a DOM element that has been subject to (direct or indirect)
            augmentation prior to being returned.
            And it should also be noted again that direct augmentation is inherently
            error-prone as host objects may implement their own internal [[Put]]
            algorithm allowed per the ECMAScript Specification. And so it can be
            observed that often code written by clueless/inexperienced people directly
            augments host objects, they not being aware that their code may break any
            minute.


            PointedEars
            --
            Anyone who slaps a 'this page is best viewed with Browser X' label on
            a Web page appears to be yearning for the bad old days, before the Web,
            when you had very little chance of reading a document written on another
            computer, another word processor, or another network. -- Tim Berners-Lee

            Comment

            • Stevo

              #7
              Re: The mothod &quot;on&quo t; not available for a button?!

              Thomas 'PointedEars' Lahn wrote:
              >document.getEl ementById('Butt on1').addEventL istener("click" ,
              >
              Reference Worms[tm] are error-prone, and should therefore be avoided.
              >
              PointedEars
              Reference worms. I like it. Did you make that up ? I've always wanted a
              term to describe what that code above is doing (incorrectly assuming
              that Button1 will be returned as an object and disregarding the
              possibility that it might return null / undefined).

              Comment

              Working...