views:

454

answers:

3

Thanks for looking, all sincerely helpful answers are up-voted. I have some date input fields that are there when the page loads and a bunch that get generated dynamically. Instead of calling .datepicker() on that class each time a new instance is generated, I'm using .live, but it doesn't seem to be working. Any idea why?

$("input[name=myfav]").live("click", function(){
    $(this).datepicker({ 
     /* some options here */ 
    });
});

I should mention, it works perfectly fine with autocomplete for instance.

$("input[name=mytwo]").live("click", function(){
    $(this).autocomplete("somefile.php");
});
+5  A: 

Here is an article about the datepicker using the .live-event in jQuery:

http://www.vancelucas.com/blog/jquery-ui-datepicker-with-ajax-and-livequery/

The problem is that the Datepicker works by binding to the focus() event by default, but as of jQuery 1.3.2, the ‘focus’ event cannot be monitored by the ‘live’ event function.

Here is the work-around from the site::

<script type="text/javascript">
$(function(){
    $('input.calendarSelectDate').live('click', function() {
     $(this).datepicker({showOn:'focus'}).focus();
    });
});
</script>
Espo
Thanks for the reply. I knew it was plugin related since the other plugins work fine. This clarifies it.
Chris
+1 you just saved me a lot of time.
Jim Schubert
+1  A: 

Worth noting that jQuery 1.4.1+ now supports focus and blur events for live(), so the workaround, whilst cool, is no longer needed - the orignal poster's version works fine!

Chris S
A: 

I use jQuery 1.4.2 with jQueryUI 1.8.4 and I have the same problem. I save the content of a div in a var, afterwards I add this again to the div and I try to activate datepicker again, but it does not work. Any idea?

function fwback(part){
    $("#new"+part+"div").html(backhtml);
    $("input[id$=date]").live("click", function(){
        $(this).datepicker();
    });
}
ralph
You should probably start a new question if you're still having this problem.
Mike Crittenden