To get old value with onchange() event in text box with JavaScript, we set the old value when we click on the input.
For instance, we write
<input
type="text"
id="test"
value="abc"
onchange="onChange(this)"
onclick="setOldValue(this)"
oldvalue=""
/>
to add an input.
We set the onchange
attribute to call the onChange
function with the input as the argument.
And we set the onclick
attribute to call the setOldValue
function with the input as the argument.
Then we write
function setOldValue(element) {
element.setAttribute("data-old-value", this.value);
}
function onChange(element) {
element.setAttribute("value", this.getAttribute("data-old-value"));
}
to define the 2 functions.
In setOldValue
, we set the data-old-value
attribute to the input value with setAttribute
.
And in onChange
, we set the value
attribute of the input to the data-old-value attribute value.