23

Is it possible to get the initial value of a form input field?

For example:

<form action="" id="formId">
    <input type="text" name="one" value="one" id="one" />
</form>

$("#one").attr("value");   //would output "one"
$("#one").val();           //would output "one"
$("#one").get(0).value;    //would output "one"

However, if the user then changed the content of the #one input field to two, the html would still look the same (with one as the value) but the JQuery calls above would return two.

JSFiddle to play with: http://jsfiddle.net/WRKHk/

I think that this must still be possible to get the orginal value, since we are able to call document.forms["formId"].reset() to reset the form to its original state.

1
  • 2
    $(this).get(0) is a ridiculous way of writing this... you really don't have to use jQuery here :P Commented Dec 13, 2011 at 14:06

7 Answers 7

35

Try this:

$("#one")[0].defaultValue;
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for defaultValue, but you had forgotten to extract the DOM element.
Thanks, RightSaidFred. Well spotted.
For checkboxes and radio buttons, you'll need to use defaultChecked instead.
16

The DOM element has the attribute "value" which you can access by:

element.getAttribute('value');

... which may be different from the element's property value.

Demo

Note that you may call .setAttribute('value', 'new value') which will actually affect any form.reset() that is called after.

10 Comments

@David But why doesn't $( this ).attr( 'value' ) return the same as this.getAttribute( 'value' )? I think it should...
@RightSaidFred You mean it's a bug? I find that hard to believe. The attr() method has been around for a long time. All bugs should have been removed by now. There must be a valid reason why attr() doesn't fully map to getAttribute()...
@ŠimeVidas: Define bug. If you expect attr() to give you the attribute value, then it was originally created with built in bugs. If you want attr() to be unclear as to whether you're dealing with the property or the attribute, then it works perfectly.
@ŠimeVidas: Exactly. You originally stated There must be a valid reason why attr() doesn't fully map to getAttribute(). But in version 1.0 there are no backward compatibility issues. ;)
@RightSaidFred I meant why it doesn't fully map now (in 1.7). It didn't map in 1.0 because there was no prop(), so attr() was designed to retrieve either the DOM property or content attribute, and in cases where both would exist, the property had precedence (since it's more important). However, since we do have prop() now, I expected attr() to fully map (at last). :/
|
9

Input element got property called defaultValue that is storing the original value.

To reach it via jQuery, just use .prop():

var value = $(this).prop("defaultValue");

Updated jsFiddle - after changing to "two", the above will return "one".

Comments

2

The HTML does change. Just that the browser's default view source tool always shows you the original source fetched from server. If you use something like Firebug, you'll be able to see the dynamic changes in HTML.

One way of getting the initial value is by storing it in an additional attribute in addition to 'value'. For example, if you serve out your HTML like:

<input type="text" name="one" value="one" origvalue="one" id="one" />

You can always get back the original value by calling:

$("#one").attr("origvalue");

1 Comment

+1 - a creative solution, but @David Hedlund's answer is the one I was looking for!
0
Save the original_values @ document.ready.

$(function(){
  var original_values = $.map($('form input'), function(input){ return $(input).val() });
})

Comments

0

You could try storing the value in data. For example:

$('#one').data('val',$('#one').val());

And then you could easily retrieve it later like this:

console.log($('#one').data('val'));

Comments

0
var initialVal = $('#one').val();
alert(initialVal);
$('#one').live("focusout", function() {
    var attribute = $("#one").attr("value");
    var value = $(this).val();
    var get = $(this).get(0).value;
    alert( "Attribute: " + attribute + "\n\nValue: " + value + "\n\ninit val: " + initialVal +"\n\nGet: " + get );
});

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.