user123456789 user123456789 - 29 days ago 12
Javascript Question

onblur is getting called before textbox gets updated

I have a textbox for a date so the user can select the date using the datepicker.

enter image description here

I want to know if the date gets changed so I added an

onblur
to the textbox:

<asp:TextBox runat="server" ID="txtDate" CssClass="dateonly" Width="80px" onblur="CheckIfDateChanged(this.value)" ></asp:TextBox>


Then in my jquery I wanted to check the date it is getting so I added an alert:

function CheckIfDateChanged(date)
{
alert(date);
}


The
onblur
seems to get called before the textbox actually changes with the new date. So if the current date is 28/10/2016 and I change it to 29/10/2016 the alert shows 28/10/2016. Then the date changes after I close the alert message.

How do I know if the date gets changed when they select the new date from the datepicker?

Answer

You can use onSelect event.

$(".dateonly").datepicker({
  onSelect: function(date) {
    display("Date: " + this.value);
  }
});

Read more https://api.jqueryui.com/datepicker/#option-onSelect