Skip to content Skip to sidebar Skip to footer

Datepicker Not Working Inside A Modal

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

Solution 1:

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();
    });
});

Solution 2:

The solution mentioned above did not work for me.

As date-picker has not the z-index property, so it was not showing on modal/popup which has the z-index. I found that the date-picker was working fine but it was showing behind the modal.So I added the z-index property to date-picker class.

.datepicker{ z-index:99999!important; }

That's why I am sharing my solution to other users. I am sure this will help someone.

Solution 3:

Add z-index to your datepicker class and add important with it. give a Greater value than modal. Normally modal have a z-index of 1050

.yourDatePicker{ z-index:9999!important; }

Solution 4:

about C#.net in .aspx

functiongetjqueryselect() {
        $("#<%=this.txtBeginEdit.ClientID%>").datepicker($.datepicker.regional["th"]);
        $("#<%=this.txtBeginEdit.ClientID%>").datepicker("setDate", newDate());
        $("#<%=this.txtEndEdit.ClientID%>").datepicker($.datepicker.regional["th"]);
        $("#<%=this.txtEndEdit.ClientID%>").datepicker("setDate", newDate());
    }

and then in Page_Load in aspx.cs

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "test", "getjqueryselect();", true);

Solution 5:

Add property container:

$('.datepickerClass').datepicker({
           ...,
            container: "#modalId",
           ...,
        });

Post a Comment for "Datepicker Not Working Inside A Modal"