Global variable undefinded? Access an object generated inside a function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • a1lostnomad
    New Member
    • Mar 2008
    • 1

    Global variable undefinded? Access an object generated inside a function?

    Hi, I'm JavaScript challenged so I'm going to need some help on this one.... I'm trying to get this accordion (based on prototype/scriptaculous) to open up a panel later in the page.

    In the head <script> tag:
    Code:
    <script type="text/javascript">
    	Event.observe(window, 'load', loadMenu, false);
    			
    	function loadMenu(){
    		sidebarMenu = new accordion('sidebar-menu');
    	}
    </script>
    Later in the page:
    Code:
    <script type="text/javascript">
    	sidebarMenu.activate($$('#sidebar-menu .accordion-toggle')[0]);
    </script>
    The error goes "sidebarMen u is not defined", which is throwing me for a loop because I thought I was defining a global in the loadMenu function. Note that the accordion works and all, it's just not activating the panel in the later code.

    The accordion code is all stock from http://www.stickmanlab s.com/accordion/

    Any help is much appreciated. Thanks.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    This is probably caused by the fact that sidebarMenu is defined on page load whereas the code later in the page runs during page load.

    Call that code after the page has loaded.

    Comment

    Working...