Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using jquery plugin for pop up calendar in asp.net page. When the page loads, it does not fires on focus event of corresponding textbox.

But, after postback the jquery calendar pop up works finely.

Please tell me the reason and solution for this.


What I have tried:

<script src="Scripts/jquery-3.1.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui.js" type="text/javascript"></script>
<link href="Scripts/jquery-ui.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">

$(document).ready(function () {
$("#txtDob").focus(function () {
$("#txtDob").datepicker({
dateFormat: "dd/mm/yy", changeMonth: true, changeYear: true
});
});
});
</script>

<asp:TextBox ID="txtDob" runat="server"></asp:TextBox>
Posted
Updated 5-Apr-17 18:55pm

try

<script type="text/javascript">

       $(document).ready(function () {
           $("#txtDob").datepicker({
               dateFormat: "dd/mm/yy", changeMonth: true, changeYear: true
           });
           $("#txtDob").focus();
       });
   </script>
 
Share this answer
 
Why do you need the focus event? This should do the trick

JavaScript
<script type="text/javascript">
    $(document).ready(function () {
       // $("#txtDob").focus(function () {
            $("#txtDob").datepicker({
                dateFormat: "dd/mm/yy", changeMonth: true, changeYear: true
            });
      //  });
    });
</script>

I see maybe you want the datepicker to show on page load by set a focus on the textbox?
How to start jquery datepicker with focus - Stack Overflow[^]
 
Share this answer
 
v2
Comments
jayesh_n 7-Apr-17 12:48pm    
Hi Bryian,

How does the above script work properly after removing focus event ? Please explain
Bryian Tan 7-Apr-17 16:42pm    
I'm not sure, I'm guessing the dynamically generated datepicker didn't get initialize correctly inside the focus function event. Another trick you can do is, explicitly call the datepicker show method after the initialization.

$("#txtDob").focus(function () {
    $("#txtDob").datepicker({
        dateFormat: "dd/mm/yy", changeMonth: true, changeYear: true
    });
    $('#txtDob').datepicker('show');
});

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900