Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery datepicker won't work on a AJAX added html element

Tags:

html

jquery

ajax

I have a jQuery datepicker function bound to the "birthday" input html element, written in the page header:

<script type="text/javascript">
    $(function() {
        $( "#birthday" ).datepicker();
    });
</script>

Next, I have some AJAX functionality - it adds new input html element to the page. That element is:

<input type="text" id="birthday" value="" class="detail-textbox1" />

Clicking on that birthday element does not pop up the date picker below the text field. I expected this, as the element is added after the page is loaded, thus it isn't in relation with the function provided in the header.

How can I make it work? I tried moving the script from the header to the body, but nothing seems to work. Thanks.

P.S. If I create an input html element with id="birthday" in the page body, everythig works as expected. It appears that only the elements added through AJAX are dysfunctional.

like image 979
Boris Avatar asked Jan 27 '11 04:01

Boris


People also ask

Why datepicker is not working in HTML?

You need to include jQuery, and include it before jQuery UI. You also need to move your code to the end of your document or wrap it in a document ready call. Include a reference to jQuery before jQuery UI.

How do I fix datepicker is not function?

(”#datepicker“).datepicker is not a function It occurs probably because of having the jquery link more than once in the file. Try searching the whole file for that duplicate content. Keep one in the <head></head> tag and remove all the rest. This will fix the issue.

How datepicker is implemented in jQuery?

The datepicker is tied to a standard form input field. Focus on the input (click, or use the tab key) to open an interactive calendar in a small overlay. Choose a date, click elsewhere on the page (blur the input), or hit the Esc key to close. If a date is chosen, feedback is shown as the input's value.

How do I know if my datepicker is loaded?

You can check to see if the datepicker script has loaded using: if ($. fn. datepicker) { // ... }


2 Answers

I'm a bit late to the party, but for thoroughness - and with the .live() function being deprecated from jQuery 1.7 onwards - I thought I'd provide an updated solution based on my experiences, and from all the help I got from other answers on StackOverflow!

I had a situation where I needed to add the datepicker functionality to input fields that were being added to the DOM through AJAX calls at random, and I couldn't modify the script making the AJAX calls to attach the datepicker functionality, so I opted for the new shiny .on() function with its delegation features:

// do this once the DOM's available...
$(function(){

    // this line will add an event handler to the selected inputs, both
    // current and future, whenever they are clicked...
    // this is delegation at work, and you can use any containing element
    // you like - I just used the "body" tag for convenience...
    $("body").on("click", ".my_input_element", function(){

        // as an added bonus, if you are afraid of attaching the "datepicker"
        // multiple times, you can check for the "hasDatepicker" class...
        if (!$(this).hasClass("hasDatepicker"))
        {
            $(this).datepicker();
            $(this).datepicker("show");
        }
    });
});

I hope this helps someone, and thanks for all the answers so far that led me to this solution that worked for me! :)

like image 63
Chris Kempen Avatar answered Oct 22 '22 07:10

Chris Kempen


You need to use .live() so that any newly added elements have the event handler attached: http://api.jquery.com/live/

$('#birthday').bind('load', function() {
    $(this).datepicker();
});

EDIT

.live() documentation states, that it is a bit out of date. With new versions of jquery (1.7+) use .on().

like image 24
JK. Avatar answered Oct 22 '22 06:10

JK.