Loading ...
Sorry, an error occurred while loading the content.

Basic Event Utility example only half working?

Expand Messages
  • Josh Lizarraga
    Check out the Basic Events section on this page: http://developer.yahoo.com/yui/event/ This is the example: var oElement =
    Message 1 of 4 , Aug 24, 2008
    • 0 Attachment
      Check out the "Basic Events" section on this page:

      http://developer.yahoo.com/yui/event/

      This is the example:

      var oElement = document.getElementById("elementid");
      function fnCallback(e) { alert("click"); }
      YAHOO.util.Event.addListener(oElement, "click", fnCallback);

      On my page, I implemented it like this, which DID NOT WORK:

      var oElement = document.getElementById("mm3_DemoPage_HeaderLogo");
      function fnCallback(e) { alert("click"); }
      YAHOO.util.Event.addListener(oElement, "click", fnCallback);

      This example, however, DID WORK:

      var oElement = document.getElementById("mm3_DemoPage_HeaderLogo");
      function fnCallback(e) { alert("click"); }
      YAHOO.util.Event.addListener("mm3_DemoPage_HeaderLogo", "click",
      fnCallback);

      ...am I missing something?
    • Josh Lizarraga
      http://autofusion.com/development/mm3/test2.html
      Message 2 of 4 , Aug 24, 2008
      • 0 Attachment
      • Satyam
        The problem is that you are setting the event listener before the element you are setting the listener on is ready. By the time your code executes, the
        Message 3 of 4 , Aug 25, 2008
        • 0 Attachment
          The problem is that you are setting the event listener before the
          element you are setting the listener on is ready. By the time your code
          executes, the element is not yet there in the page,
          document.getElementById will simply return null. However, if you call
          Event.addListener using the name of the object, if the object is not
          found, the Event utility will start a timer and check the object a
          little later when there is a chance that the element might be there.

          You should not execute code relying on HTML element without making sure
          those elements do exist. See:

          http://developer.yahoo.com/yui/event/#onavailable



          Satyam


          Josh Lizarraga wrote:
          > Check out the "Basic Events" section on this page:
          >
          > http://developer.yahoo.com/yui/event/
          >
          > This is the example:
          >
          > var oElement = document.getElementById("elementid");
          > function fnCallback(e) { alert("click"); }
          > YAHOO.util.Event.addListener(oElement, "click", fnCallback);
          >
          > On my page, I implemented it like this, which DID NOT WORK:
          >
          > var oElement = document.getElementById("mm3_DemoPage_HeaderLogo");
          > function fnCallback(e) { alert("click"); }
          > YAHOO.util.Event.addListener(oElement, "click", fnCallback);
          >
          > This example, however, DID WORK:
          >
          > var oElement = document.getElementById("mm3_DemoPage_HeaderLogo");
          > function fnCallback(e) { alert("click"); }
          > YAHOO.util.Event.addListener("mm3_DemoPage_HeaderLogo", "click",
          > fnCallback);
          >
          > ...am I missing something?
          >
          >
          > ------------------------------------
          >
          > Yahoo! Groups Links
          >
          >
          >
          >
          > No virus found in this incoming message.
          > Checked by AVG - http://www.avg.com
          > Version: 8.0.138 / Virus Database: 270.6.7/1631 - Release Date: 24/08/2008 12:15
          >
          >
          >
          >
        • Josh Lizarraga
          Thanks a ton! ... code ... 24/08/2008 12:15
          Message 4 of 4 , Aug 25, 2008
          • 0 Attachment
            Thanks a ton!

            --- In ydn-javascript@yahoogroups.com, Satyam <satyam@...> wrote:
            >
            > The problem is that you are setting the event listener before the
            > element you are setting the listener on is ready. By the time your
            code
            > executes, the element is not yet there in the page,
            > document.getElementById will simply return null. However, if you call
            > Event.addListener using the name of the object, if the object is not
            > found, the Event utility will start a timer and check the object a
            > little later when there is a chance that the element might be there.
            >
            > You should not execute code relying on HTML element without making sure
            > those elements do exist. See:
            >
            > http://developer.yahoo.com/yui/event/#onavailable
            >
            >
            >
            > Satyam
            >
            >
            > Josh Lizarraga wrote:
            > > Check out the "Basic Events" section on this page:
            > >
            > > http://developer.yahoo.com/yui/event/
            > >
            > > This is the example:
            > >
            > > var oElement = document.getElementById("elementid");
            > > function fnCallback(e) { alert("click"); }
            > > YAHOO.util.Event.addListener(oElement, "click", fnCallback);
            > >
            > > On my page, I implemented it like this, which DID NOT WORK:
            > >
            > > var oElement = document.getElementById("mm3_DemoPage_HeaderLogo");
            > > function fnCallback(e) { alert("click"); }
            > > YAHOO.util.Event.addListener(oElement, "click", fnCallback);
            > >
            > > This example, however, DID WORK:
            > >
            > > var oElement = document.getElementById("mm3_DemoPage_HeaderLogo");
            > > function fnCallback(e) { alert("click"); }
            > > YAHOO.util.Event.addListener("mm3_DemoPage_HeaderLogo", "click",
            > > fnCallback);
            > >
            > > ...am I missing something?
            > >
            > >
            > > ------------------------------------
            > >
            > > Yahoo! Groups Links
            > >
            > >
            > >
            > >
            > > No virus found in this incoming message.
            > > Checked by AVG - http://www.avg.com
            > > Version: 8.0.138 / Virus Database: 270.6.7/1631 - Release Date:
            24/08/2008 12:15
            > >
            > >
            > >
            > >
            >
          Your message has been successfully submitted and would be delivered to recipients shortly.