Pekka Pekka - 9 months ago 320
Javascript Question

Input jquery get old value before onchange and get value after on change

I have an input text in jquery i want to know if it possible to get the value of that input text(type=number and type=text) before the onchange happens and also get the value of the same input input text after the onchange happens. This is using jquery.

What i tried

I tried saving the value on variable then call that value inside onchange but i am getting blank value


The simplest way is to save the original value using data() when the element gets focus. Here is a really basic example:


$('input').on('focusin', function(){
    console.log("Saving value " + $(this).val());
    $(this).data('val', $(this).val());

$('input').on('change', function(){
    var prev = $(this).data('val');
    var current = $(this).val();
    console.log("Prev value " + prev);
    console.log("New value " + current);