With the latest changes to jQuery there are the different methods, .prop()
.attr()
.
What I am wondering is if there is a way to get the original value of the input when the page was loaded?
I know I could do:
$('#input').data('originalValue', $('#input').val());
and then I can refer to it through
$('#input').data('originalValue');
But I thought that since I can say, find out the original checked state of a checkbox I thought maybe there is something similar for the value?
You can use the input element's defaultValue
property
For example:
$('#input')[0].defaultValue
Note how by treating the jQuery object as an array we are able to directly access the DOM node and its native property.
See jsFiddle demo
You can use jQuery.get
to get the element and grab the .defaultValue
property:
$('#input').get(0).defaultValue
Or use jQuery.prop
since jQuery 1.6:
$('#input').prop("defaultValue");
Likewise for defaultChecked
(radio and checkbox) and defaultSelected
(option).
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