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.
The DOM element has the attribute "value" which you can access by:
... which may be different from the element's property
value
.Demo
Note that you may call
.setAttribute('value', 'new value')
which will actually affect anyform.reset()
that is called after.Input element got property called
defaultValue
that is storing the original value.To reach it via jQuery, just use
.prop()
:Updated jsFiddle - after changing to "two", the above will return "one".
Try this:
You could try storing the value in data. For example:
And then you could easily retrieve it later like this: