views:

569

answers:

4

So I'm using the jQuery date picker, and it works well. I am using AJAX to go and get some content, obviously when this new content is applied the bind is lost, I learnt about this last week and discovered about the .live() method.

But how do I apply that to my date picker? Because this isn't an event therefore .live() won't be able to help... right?

This is the code I'm using to bind the date picker to my input:

$(".datefield").datepicker({showAnim:'fadeIn',dateFormat:'dd/mm/yy',changeMonth:true,changeYear:true});

I do not want to call this metho everytime my AJAX fires, as I want to keep that as generic as possible.

Cheers :-)

EDIT

As @nick requested, below is my wrapper function got the ajax() method:

var ajax_count = 0;
function getElementContents(options) {
    if(options.type===null) {
         options.type="GET";
    }

    if(options.data===null) {
        options.data={};
    }

    if(options.url===null) {
        options.url='/';
    }

    if(options.cache===null) {
        options.cace=false;
    }

    if(options.highlight===null || options.highlight===true) {
        options.highlight=true;
    } else {
        options.highlight=false;
    }

    $.ajax({
        type: options.type,
        url: options.url,
        data: options.data,
        beforeSend: function() {
            /* if this is the first ajax call, block the screen */
            if(++ajax_count==1) {
                $.blockUI({message:'Loading data, please wait'});
            } 
        },
        success: function(responseText) {
            /* we want to perform different methods of assignment depending on the element type */

            if($(options.target).is("input")) {
                $(options.target).val(responseText);
            } else {
                $(options.target).html(responseText);
            }
            /* fire change, fire highlight effect... only id highlight==true */
            if(options.highlight===true) {
                $(options.target).trigger("change").effect("highlight",{},2000);
            }
        },
        complete: function () {
            /* if all ajax requests have completed, unblock screen */
            if(--ajax_count===0) {
                $.unblockUI();
            }
        },
        cache: options.cache,
        dataType: "html"
    });
}

What about this solution, I have a rules.js which include all my initial bindings with the elements, if I were to put these in a function, then call that function on the success callback of the ajax method, that way I wouldn't be repeating code...

Hmmm, thoughts please :D

A: 

First port of call would be to read up on this question, if you haven't already:
http://stackoverflow.com/questions/2386718/jquery-live-failing-with-jquery-ui-datepicker

"I've got it to bind to the lightbox'd inputs on the first appearance of the lightbox, but it's not working afterwards."

"When the lightbox closes, I'm re-appending it's content to the page (in order to not lose the content div), and this seems to be killing the live() call."

GlenCrawford
I don't understand how this relates to me, as it doesn't provide clues or an answer?
ILMV
A: 

date picker is a click function on the text input mate. so you can easily go:

$('#datepicker').live('click', function(){$(this).datepicker($.datepicker.regional['en']);})
XGreen
This will create a date picker widget every click on the element, this would be extremely wasteful.
Nick Craver
+1 Nick, I wanted to avoid this method of possible. Thanks anyway XGreen.
ILMV
Didn't know this. Thanks for correcting nick
XGreen
+2  A: 

You can do it like this:

function createPickers(context) {
  $(".datefield", context || document).datepicker({
    showAnim:'fadeIn',
    dateFormat:'dd/mm/yy',
    changeMonth:true,
    changeYear:true
  });
}

To run on document.ready, if you already have a document.ready function you can call:

createPickers();

Or you can run it in it's own document.ready handle, like this:

$(createPickers);

In your success callback you call it like this:

createPickers(responseText);

What this does is only select .datefield in the provided context (internally this uses .find()), so on document.ready you're selecting all matching elements to create date pickers, but in your ajax request, you're selecting only the matching elements that just arrived in the response, not creating duplicate date pickers anywhere.

Nick Craver
Ok this looks like the best solution. Nick would you suggest all my binding rules (such as click, change etc) are contained within one function, that I can then call on the success callback? Would save me having to use `.live()` then? Just wondering about the limitations of `.live()`. Thanks :-)
ILMV
@ILMV - If you're not creating widgets with those other events I'd use `.live()`, cleaner, simpler, and less event handlers. For the things that create widgets like the `datepicker`, yes I'd add these into a single function that takes a context like this.
Nick Craver
Thank you very much for you're help Nick!
ILMV
A: 

Take a look at this:

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

miguelfteixeira
This is the exact same example XGreen gave,
ILMV