putting datepicker() on dynamically created elements - JQuery/JQueryUI
For me below jquery worked:
changing "body" to document
$(document).on('focus',".datepicker_recurring_start", function(){
$(this).datepicker();
});
Thanks to skafandri
Note: make sure your id is different for each field
here is the trick:
$('body').on('focus',".datepicker_recurring_start", function(){
$(this).datepicker();
});
DEMO
The $('...selector..').on('..event..', '...another-selector...', ...callback...);
syntax means:
Add a listener to ...selector..
(the body
in our example) for the event ..event..
('focus' in our example). For all the descendants of the matching nodes that matches the selector ...another-selector...
(.datepicker_recurring_start
in our example) , apply the event handler ...callback...
(the inline function in our example)
See http://api.jquery.com/on/ and especially the section about "delegated events"
Excellent answer by skafandri +1
This is just updated to check for hasDatepicker class.
$('body').on('focus',".datepicker", function(){
if( $(this).hasClass('hasDatepicker') === false ) {
$(this).datepicker();
}
});