SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    runat="server" Golgotha's Avatar
    Join Date
    Nov 2001
    Location
    Colorado
    Posts
    2,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    EventListener help

    Testing the below code in Firefox does not work?
    Firebug says, "BtnExternalFeeds has no properties"

    Any ideas?

    Code:
    var setExternalFeeds = 
    { 
        init: function()
        {       
            var BtnExternalFeeds = document.getElementById("BtnExternalFeeds");
            if (typeof BtnExternalFeeds.addEventListener != "undefined")
            {
                alert("test");
                BtnExternalFeeds.addEventListener("click", setExternalFeeds.clickListener, false);
            }     
        },
          
        clickListener: function()
        {
            alert(Feeds.length);
        }
    };
    // set the page load handler
    if(typeof window.addEventListener != 'undefined')
    {
        // not IE
        window.addEventListener('load', setExternalFeeds.init(), false);
    } else {
        // IE
        window.attachEvent('onload', setExternalFeeds.init());
    }

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You don't need to execute the function within your addEventListener. Instead, you have to provide a function reference:
    Code Javascript:
    var setExternalFeeds = 
    { 
        init: function()
        {       
            var BtnExternalFeeds = document.getElementById("BtnExternalFeeds");
            if (window.addEventListener)
            {
                alert("test");
                BtnExternalFeeds.addEventListener("click", setExternalFeeds.clickListener, false);
            }     
        },
     
        clickListener: function()
        {
            alert(Feeds.length);
        }
    };
    // set the page load handler
    if(window.addEventListener)
    {
        // not IE
        window.addEventListener('load', setExternalFeeds.init, false);
    } else {
        // IE
        window.attachEvent('onload', setExternalFeeds.init);
    }
    If you still get an error, check that you have the right ID for BtnExternalFeeds.

  3. #3
    runat="server" Golgotha's Avatar
    Join Date
    Nov 2001
    Location
    Colorado
    Posts
    2,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    see below...
    Last edited by Golgotha; Dec 8, 2007 at 19:53.

  4. #4
    runat="server" Golgotha's Avatar
    Join Date
    Nov 2001
    Location
    Colorado
    Posts
    2,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It now accepts the eventListener, but when clicked it throws the error and says, "btnExternalFeeds is not defined"...

    error is on the line with btnExternalFeeds.value = "click me";

    doesn't window.onload happen only after the entire page is loaded?

    why is it then not defined?


    Code JavaScript:
    var setExternalFeeds = 
    { 
        init: function()
        {       
            var feeds = document.getElementById("externalFeeds").getElementsByTagName("div");
            var btnExternalFeeds = document.getElementById("BtnExternalFeeds");
     
     
            if (typeof btnExternalFeeds.addEventListener != "undefined")
            {
                btnExternalFeeds.addEventListener("click", setExternalFeeds.clickListener, false);
            }     
        },
     
        clickListener: function()
        {
            //alert("test");
            btnExternalFeeds.value = "click me";
        }
    };
    // set the page load handler
    if(typeof window.addEventListener != 'undefined')
    {
        // not IE
        window.addEventListener('load', setExternalFeeds.init, false);
    } else {
        // IE
        window.attachEvent('onload', setExternalFeeds.init);
    }

  5. #5
    runat="server" Golgotha's Avatar
    Join Date
    Nov 2001
    Location
    Colorado
    Posts
    2,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone?

  6. #6
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    365
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    change

    Code:
    btnExternalFeeds.value =  "click me";
    to

    Code:
    document.getElementById("BtnExternalFeeds").value =  "click me";

  7. #7
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    365
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or
    Code JavaScript:
    var setExternalFeeds = 
    {
       btnExternalFeeds : null,
       init: function()    
       {
            var feeds = document.getElementById("externalFeeds").getElementsByTagName("div");
            this.btnExternalFeeds = document.getElementById("BtnExternalFeeds");
            if (typeof this.btnExternalFeeds.addEventListener != "undefined")
            {            
                     this.btnExternalFeeds.addEventListener("click", setExternalFeeds.clickListener, false);
            }
        }, 
     
        clickListener: function()    
        {        
              //alert("test");
             this.btnExternalFeeds.value = "click me";
        }
     
    };// set the page load handler
     
    if(typeof window.addEventListener != 'undefined')
    {    // not IE    
               window.addEventListener('load', setExternalFeeds.init, false);
    }
     else 
    {    // IE    
           window.attachEvent('onload', setExternalFeeds.init);
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •