4

For example we have html input with value="wally":

<input type="text" value="wally" />

How to get this old "wally" value (using jQuery) after user change it?

I tried to get it with .attr(), .prop(), .val() but all of them return the new value, which user entered.

You may test the code on jsfiddle. Try to change input value and click "get" blue button.

P.S. jQuery 1.7.2 included into the jsfiddle.

6 Answers 6

5

Use defaultValue of the underlying DOM element:

var input = $(this).closest('form').find('input');
console.log(input[0].defaultValue);

Updated fiddle

And of course, if you're using jQuery 1.6 or higher, you can use prop('defaultValue') for that (and in earlier versions, attr('defaultValue') probably works):

var input = $(this).closest('form').find('input');
console.log(input.prop('defaultValue'));

Updated fiddle

Sign up to request clarification or add additional context in comments.

4 Comments

You probably have a bit more experience with it, are there any cross-browser issues with .getAttribute('value')?
@FelixKling: I wouldn't trust IE as far as I could throw Steve Ballmer, particularly not IE7. Whereas I know that defaultValue has worked for 15 some-odd years. I think I tried getAttribute and found it to be unreliable, but I'd have to do an experiment to be sure.
@FelixKling: Yeah. I went with Ballmer 'cause he's bigger than Gates. :-)
@FelixKling: Yeah, IE gets getAttribute('value') wrong, all the way through IE8 (even when IE8 is in "standards" mode). IE9 in standards mode gets it right. jsbin.com/usudet
4

To remember the value before it was changed..

$('input').focusin(function(){
   $(this).data('oldValue', $(this).val());
});

And get it back using..

$('input').change(function(){
   var old = $(this).data('oldValue'); //old value
   var new = $(this).val(); //new value
})

See working example here.

1 Comment

Try to change input value few times and each time "oldValue" will be changed. Better to remember "oldValue" once after page loaded.
0

Use element.defaultValue; Where element is of course the input field. If using JavaScript to fill the element in the first place, just add a new data attribute.

For example data-defaultValue='wallie'

Comments

0

You could store default value in a data-container for the input element, then update that data-value when user clicks the get button (unless you always want to have access to default value and not just the last value).

Fiddle: http://jsfiddle.net/DA67p/4/

1 Comment

Try to change input value few times and each time "oldValue" will be changed. Better to remember "oldValue" once after page loaded.
-1

You could use another attribute like :

<input type="text" value="wally" oldVal="wally" />

And then use

input.attr('oldVal');

1 Comment

oldVal is not a valid attribute. data-oldval would be.
-1

There are enough answers to this question. But there is another work around which will be helpful if you need the old value in code behind or server side.

Add a hidden field with the same value to your page. So if you need the old value use hidden field's value. Doing this you will get the old value both in client side as well as server side.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.