Sometimes, we want to get old value with onchange() event in text box with JavaScript.
In this article, we’ll look at how to get old value with onchange() event in text box with JavaScript.
How to get old value with onchange() event in text box with JavaScript?
To get old value with onchange() event in text box with JavaScript, we can get the old value when the click event is emitted.
For instance, we add an input with
<input type="text" id="test" value="ABS" oldvalue="" />
Then we write
const input = document.querySelector("input");
input.onclick = (e) => {
e.target.setAttribute("oldvalue", e.target.vakue);
};
input.onchange = (e) => {
e.target.setAttribute("value", e.target.getAttribute("oldvalue"));
};
to select the input with querySelector
.
Then we set input.onclick
to a function that gets the input value when we click it.
We get the old input value with e.target.value
.
Next we set input.onchange
to a function that calls e.target.setAttribute
to set the value
attribute to the oldvalue
attribute’s value we get with getAttribute
.
Conclusion
To get old value with onchange() event in text box with JavaScript, we can get the old value when the click event is emitted.