Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input jQuery get old value before onchange and get value after on change

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.

like image 223
Pekka Avatar asked Mar 18 '15 09:03

Pekka


People also ask

How do I get past value on Onchange?

To get old value with onchange() event in text box with JavaScript, we can get the old value when the click event is emitted.

How do I use Onchange input field?

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.

What triggers Onchange event?

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.


2 Answers

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); }); 

Better to use Delegated Event Handlers

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 :)

like image 160
Gone Coding Avatar answered Oct 01 '22 07:10

Gone Coding


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.

like image 22
mashi Avatar answered Oct 01 '22 06:10

mashi