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 a blank value.
To get old value with onchange() event in text box with JavaScript, we can get the old value when the click event is emitted.
The onchange attribute fires the moment when the value of the element is changed. Tip: This event is similar to the oninput event. The difference is that the oninput event occurs immediately after the value of an element has changed, while onchange occurs when the element loses focus.
The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the checked state has been changed.
The simplest way is to save the original value using data()
when the element gets focus. Here is a really basic example:
JSFiddle: http://jsfiddle.net/TrueBlueAussie/e4ovx435/
$('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); });
Note: it is generally more efficient to use a delegated event handler when there can be multiple matching elements. This way only a single handler is added (smaller overhead and faster initialisation) and any speed difference at event time is negligible.
Here is the same example using delegated events connected to document
:
$(document).on('focusin', 'input', function(){ console.log("Saving value " + $(this).val()); $(this).data('val', $(this).val()); }).on('change','input', function(){ var prev = $(this).data('val'); var current = $(this).val(); console.log("Prev value " + prev); console.log("New value " + current); });
JsFiddle: http://jsfiddle.net/TrueBlueAussie/e4ovx435/65/
Delegated events work by listening for an event (focusin
, change
etc) on an ancestor element (document
* in this case), then applying the jQuery filter (input
) to only the elements in the bubble chain then applying the function to only those matching elements that caused the event.
*Note: A a general rule, use document
as the default for delegated events and not body
. body
has a bug, to do with styling, that can cause it to not get bubbled mouse events. Also document
always exists so you can attach to it outside of a DOM ready handler :)
Definitely you will need to store old value manually, depending on what moment you are interested (before focusing, from last change). Initial value can be taken from defaultValue property:
function onChange() { var oldValue = this.defaultValue; var newValue = this.value; }
Value before focusing can be taken as shown in Gone Coding's answer. But you have to keep in mind that value can be changed without focusing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With