4

How to get old value and new value from input type text;

<input type="text" name="quantity-row_111" value="1" id="quantity-row_111" onchange="myfunction(id);">

function myfunction(id){
     var oldvalue = getoldvalue();
     var newvalue = getnewvalue();
}
Shojib Flamon
  • 1,237
  • 3
  • 12
  • 18
  • can you clarify what did you mean by `old` and `new` value? and also where is your `getoldvalue()` and `getnewvalue()` function? – Md Ashaduzzaman Jan 31 '15 at 10:21
  • And what do you mean by "old" value, the previous typed in value, or the value given in the attribute ? – adeneo Jan 31 '15 at 10:22
  • 1
    possible duplicate of [How to get old Value with onchange() event in text box](http://stackoverflow.com/questions/1909992/how-to-get-old-value-with-onchange-event-in-text-box) – Blauharley Jan 31 '15 at 10:29
  • You should pick an answer, you are still an active user but no correct answer has been chosen. – ADJenks Mar 27 '19 at 18:56

5 Answers5

5

Try this,

HTML

 <input type="text" onfocus="this.oldvalue = this.value;" onchange="onChangeTest(this);this.oldvalue = this.value;" />

JavaScript

function onChangeTest(textbox) {
      console.log("New value: " + textbox.value + "\n" + "Old value: " + textbox.oldvalue);
}

Reference

Community
  • 1
  • 1
super
  • 2,288
  • 2
  • 21
  • 23
1

You may add another attribute (prevvalue) in your input fields like so:

function myfunction(id){
  var oldvalue = id.getAttribute("prevvalue");
  var newvalue = id.value;
  alert(oldvalue);
  alert(newvalue);
}
    <input type="text" name="quantity-row_111" value="1" prevvalue="4.50" id="quantity-row_111" onchange="myfunction(this);">

Then you have access to both values whenever any change occurs. You will need to either pre-populate the old value or update it whenever the new value changes. Cheers.

lasbreyn
  • 141
  • 1
  • 4
1

If your element id is quantity-row_111, you can get the original value and the new value by :

console.log("new value="+ document.getElementById('quantity-row_111').value  ); 
console.log("Original value="+ document.getElementById('quantity-row_111').defaultValue);

Look at the console log for the result.

Florent
  • 436
  • 3
  • 8
0

Keep the newvalue saved as your oldvalue. This might be what you what you want,

var oldvalue = document.getElementById("quantity-row_111").value;
var newvalue;
function myfunction(id){
    alert(oldvalue);
    newvalue = document.getElementById("quantity-row_111").value;
    oldvalue = newvalue;
}

jsFiddle

Md Ashaduzzaman
  • 4,032
  • 2
  • 18
  • 34
0

you should try something like this:

$(document).ready(function () {
        var OldValue;
        $("form :input").click(function () {
            var field= ($(this).attr('name'));
            OldValue= ($(this).attr('field'));
        })


        $("form :input").change(function () {
            var field = ($(this).attr('name'));
            var value = ($(this).attr('value'));
            console.log('Field:: ' + field + '\nNew Value: ' + value + '\nOld Value: ' + OldValue+'\n--------------------\n');

        })
    })