Albert Prats
Albert Prats

Reputation: 796

Jquery change event fired before value is set

I got two date inputs where the user sets a start date and a end date. After that i got a label where I load the number of days between the two dates that the user selected.

The two date input got a the same class: "fecha" and I handle the data input and the number of days operation like this:

$('.fecha').live("change", function(){

    var fecha1 = $('#E2Del').val();
    var fecha2 = $('#E2Al').val();

    var ini_split = fecha1.split('-');
    var ini_full = ini_split[0]+ini_split[1]+ini_split[2];

    var fin_split = fecha2.split('-');
    var fin_full = fin_split[0]+fin_split[1]+fin_split[2];

    var test = fin_full - ini_full;        
    console.log(test);
  });

everyting works good, the problem is that this event is fired when I click on the date input, not after i changed the selected date on each input. So to make this works I need to click on any of the two inputs after i set the values.

Any idea on how to make this event fires after the date input and not when i click on the element ?

Thanks in advance

Upvotes: 3

Views: 3527

Answers (1)

Barney
Barney

Reputation: 16456

The reason you're seeing the old value is that when the change event fires, the native consequences (namely, changing the value) haven't occurred yet — in fact you could cancel them with e.preventDefault().

What you would need to do is examine the event itself:

e.originalEvent.newValue

...will return the value that is being assigned.

Upvotes: 1

Related Questions