views:

296

answers:

1

Hello, could someone explain too me how this code will work

Because, I don't know if I should put the code in the plugin-file or de head section off the page

what else do I need too pay attention for

the code is from http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/datePickerCloseMouseOut.html

thanks in advance, Richard

(also, I don't know wat cal is referring too,datePickerDiv and $('.date-pick')?)

 $(function() 
{ 
   var cal; 
   var $this; 

   var checkForMouseout = function(event) 
   { 
      var el = event.target; 

      while (true){ 
         if (el == cal) { 
            return true; 
         } else if (el == document) { 
            $this.dpClose(); 
            return false; 
         } else { 
            el = $(el).parent()[0]; 
         } 
      } 
   }; 

   $('.date-pick') 
      .datePicker() 
      .bind( 
         'dpDisplayed', 
         function(event, datePickerDiv) 
         { 
            cal = datePickerDiv; 
            $this = $(this); 
            $(document).bind( 
               'mouseover', 
               checkForMouseout 
            ); 
         } 
      ).bind( 
         'dpClosed', 
         function(event, selected) 
         { 
            $(document).unbind( 
               'mouseover', 
               checkForMouseout 
            ); 
         } 
      ); 

});
+1  A: 

This code will check if the mouse leaves the datepicker div and then close it if the mouse did leave. The code checks this by checking if the element that received the event was the calendar.

//el is set above or below, call is set globally in the document.ready
while (true){ //this will loop forever until a return
     if (el == cal) { //is the receiving element the calender
        return true; //we return true (no ideo why true and not null or 'yaadada'
     } else if (el == document) { //we check if the target el is the document
        $this.dpClose();  //close the element
        return false; //return to leave loop
     } else { //el is neither the call or the document
        el = $(el).parent()[0]; //set el to the imidiate parent of the current el and reloop
     }
}

You should put this code in the head of your document.

Easier would be this code:

$('.date-pick') 
  .datePicker() 
  .bind( 
     'dpDisplayed', 
     function(event, datePickerDiv) 
     { 
        cal = datePickerDiv; //datepickerdiv should somehow hold the the datpicker div , something like: $('.date-pick')[0];
        $this = $(this); 
        $(cal).mouseleave( function() { $(this).dpClose(); });
     }
}

A better question might be, why do you wan't to include code of which you don't know what it does?

NOTE: this code is very ugly and you should propably consider rewriting it.

Pim Jager
thanks, you are partially right I am new too jquery, so I don't understand the syntax completely If you say it is ugly, I just have too take your word for it. But what I really want, is too have the functionality provided in the demo page of the datepicker, but it is not providing information on how to ajust it too make it work. http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/datePickerCloseMouseOut.htmlI am a little bit lost here. Y found a div id inside the plugin file that is used too create the popup calendar, but I can't make it work
I hope somebody could take a look at the whole script, because I can't figure it out. So for the meantime I gave up on the idea that I could make it work. I emaild the creator too see if I can get A reaction from him.If you see the demo page sourcecode, you will see it is using the exact same script without modifications, and for some reason it works on that page. Therefore, it seems pointless too break my head over it. I am clueless at this point.