How to get old Value with onchange() event in text box

JavascriptHtmlValidation

Javascript Problem Overview


I have a text input. A value is populated into it when the page loads. If the user changes anything in text box, then I want to get the changed value (the new value) and old value. But calling ELEMENT.value it only returns the changed/new value.

How do I get the old value?

Here is my code:

      <head>
        <script type="text/javascript">
          function onChangeTest(changeVal) {
            alert("Value is " + changeVal.value);
          }
        </script>
      </head>
      <body>
        <form>
          <div>
              <input type="text" id="test" value ="ABS" onchange="onChangeTest(this)">  
          </div>
        </form>
      </body>

Javascript Solutions


Solution 1 - Javascript

You'll need to store the old value manually. You could store it a lot of different ways. You could use a javascript object to store values for each textbox, or you could use a hidden field (I wouldn't recommend it - too html heavy), or you could use an expando property on the textbox itself, like this:

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

Then your javascript function to handle the change looks like this:

    <script type="text/javascript">
	function onChangeTest(textbox) {
		alert("Value is " + textbox.value + "\n" + "Old Value is " + textbox.oldvalue);
	}
    </script>

Solution 2 - Javascript

element.defaultValue will give you the original value.

Please note that this only works on the initial value.

If you are needing this to persist the "old" value every time it changes, an expando property or similar method will meet your needs

Solution 3 - Javascript

You should use HTML5 data attributes. You can create your own attributes and save different values in them.

Solution 4 - Javascript

I would suggest:

function onChange(field){
  field.old=field.recent;
  field.recent=field.value;

  //we have available old value here;
}

Solution 5 - Javascript

A dirty trick I somtimes use, is hiding variables in the 'name' attribute (that I normally don't use for other purposes):

select onFocus=(this.name=this.value) onChange=someFunction(this.name,this.value)><option...

Somewhat unexpectedly, both the old and the new value is then submitted to someFunction(oldValue,newValue)

Solution 6 - Javascript

You can do this: add oldvalue attribute to html element, add set oldvalue when user click. Then onchange event use oldvalue.

<input type="text" id="test" value ="ABS" onchange="onChangeTest(this)" onclick="setoldvalue(this)" oldvalue="">

<script>
function setoldvalue(element){
   element.setAttribute("oldvalue",this.value);
}

function onChangeTest(element){
   element.setAttribute("value",this.getAttribute("oldvalue"));
}
</script>

Solution 7 - Javascript

I am not sure, but maybe this logic would work.

var d = 10;
var prevDate = "";
var x = 0;
var oldVal = "";
var func = function (d) {
    if (x == 0 && d != prevDate && prevDate == "") {
        oldVal = d;
        prevDate = d;
    }
    else if (x == 1 && prevDate != d) {
        oldVal = prevDate;
        prevDate = d;
    }
    console.log(oldVal);
    x = 1;
};
/*
         ============================================
         Try:
         func(2);
         func(3);
         func(4);
*/

Solution 8 - Javascript

Maybe you can store the previous value of the textbox into a hidden textbox. Then you can get the first value from hidden and the last value from textbox itself. An alternative related to this, at onfocus event of your textbox set the value of your textbox to an hidden field and at onchange event read the previous value.

Solution 9 - Javascript

Maybe you can try to save the old value with the "onfocus" event to afterwards compare it with the new value with the "onchange" event.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCFUserView Question on Stackoverflow
Solution 1 - JavascriptGabriel McAdamsView Answer on Stackoverflow
Solution 2 - JavascriptGeorgeView Answer on Stackoverflow
Solution 3 - Javascripttiwari.vikashView Answer on Stackoverflow
Solution 4 - JavascriptkrsnikView Answer on Stackoverflow
Solution 5 - JavascriptJonView Answer on Stackoverflow
Solution 6 - JavascriptPham Thai SonView Answer on Stackoverflow
Solution 7 - JavascriptManjeetView Answer on Stackoverflow
Solution 8 - JavascriptCanavarView Answer on Stackoverflow
Solution 9 - JavascriptownkingView Answer on Stackoverflow