Can't implement listener

YAHOO.namespace("yuibook.calendar"); 

//define the lauchCal function which creates the calendar 
YAHOO.yuibook.calendar.launchCal = function() { 

    //create the calendar object, specifying the container 
    var myCal = new YAHOO.widget.Calendar("mycal"); 

    //draw the calendar on screen 
    myCal.render(); 
}

//define the showCal function which shows the calendar
Var showCal = function() { 
    //show the calendar 
    myCal.show(); 
} 

//create calendar on page load
YAHOO.util.Event.onDOMReady(YAHOO.yuibook.calendar.launchCal); 

//attach listener for click event on calendar icon 
YAHOO.util.Event.addListener("calico", "click", showCal); 

//myCal.hide();

      

The problem with the code is that I cannot display the calendar when I click the icon. can anyone help. I know this listener

-2


source to share


1 answer


The problem is that the variable is myCal

undefined in the function showCal

. A variable is myCal

defined in a function launchCal

and is only accessible from the scope of that function. You will need to keep a reference to your variable myCal

.

Something like the following should do the trick:



YAHOO.namespace("yuibook.calendar");

YAHOO.util.Event.onDOMReady(function() {
    YAHOO.yuibook.calendar.myCal = new YAHOO.widget.Calendar("mycal");
    YAHOO.yuibook.calendar.myCal.render();

    YAHOO.util.Event.addListener("calico", "click", YAHOO.yuibook.calendar.myCal.show); 
});

      

Check out this popup calendar example from the YUI documentation for more details.

+1


source







All Articles