I have dynamically created textboxes, and I want each of them to be able to display a calendar on click. The code I am using is:
$(".datepicker_recurring_start" ).datepicker();
Which will only work on the first textbox, even though all my textboxes have a class called datepicker_recurring_start.
Your help is much appreciated!
here is the trick:
DEMO
The
$('...selector..').on('..event..', '...another-selector...', ...callback...);
syntax means:Add a listener to
...selector..
(thebody
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"
For me below jquery worked:
changing "body" to document
Thanks to skafandri
Note: make sure your id is different for each field
This is what worked for me on JQuery 1.3 and is showing on the first click/focus
this needs that your input have the class 'mostrar_calendario'
Live is for JQuery 1.3+ for newer versions you need to adapt this to "on"
See more about the difference here http://api.jquery.com/live/
Excellent answer by skafandri +1
This is just updated to check for hasDatepicker class.
You need to run the
.datepicker();
again after you've dynamically created the other textbox elements.I would recommend doing so in the callback method of the call that is adding the elements to the DOM.
So lets say you're using the JQuery Load method to pull the elements from a source and load them into the DOM, you would do something like this:
None of the other solutions worked for me. In my app, I'm adding the date range elements to the document using jquery and then applying datepicker to them. So none of the event solutions worked for some reason.
This is what finally worked:
Hope this helps someone because this set me back a bit.