Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
55
rated 0 times [  58] [ 3]  / answers: 1 / hits: 39222  / 11 Years ago, thu, august 29, 2013, 12:00:00

I have a site and here is a link, When you click on textfield you can see the DatePicker working
But iF you click on ImportFriend -> Add Manual Friend -> then if you click on birthday field the datepicker never appears.I can see through firebug that the field got the hasDatePicker attribute.
I am not sure what making it not to show the datepicker,anyone who can help me please thanks


More From » jquery

 Answers
21

As @Bluetoft says, the answer is event delegation.



The reason it's not working for you is because your datepicker is being bound to elements that exist at the time the script is run (not to mention, the birthday field has a different id than #datepicker, which is what your script is binding to).



When you dynamically bring in the new form, the datepicker is not bound to the new elements.



So, one method, according to this answer, is to construct your script like so:



$(function() {
$(body).delegate(#datepicker, focusin, function(){
$(this).datepicker();
});
});


Additionally, your form birthday input has an id of #fi_bday, which is another reason it's not being bound. I'd recommend that you assign any inputs where you want the datepicker with a class of datepicker, then change your script to bind the datepicker functionality to '.datepicker' elements:



$(function() {
$(body).delegate(.datepicker, focusin, function(){
$(this).datepicker();
});
});


Finally, if you don't want to utilize the better, generic class method above, you can use two selectors in the selector below. The method below delegates in a manner as to bind the datepicker to the modal window element(s) as well:



$(function() {
$(body).delegate(#datepicker, #fi_bday, focusin, function(){
$(this).datepicker();
});
});

[#76021] Thursday, August 29, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emmaleet

Total Points: 203
Total Questions: 107
Total Answers: 98

Location: Cook Islands
Member since Thu, May 21, 2020
4 Years ago
;