Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does an input have an "original value" attribute?

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?

like image 1000
Hailwood Avatar asked Oct 16 '25 13:10

Hailwood


2 Answers

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

like image 173
Boaz Avatar answered Oct 18 '25 10:10

Boaz


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

like image 20
Salman A Avatar answered Oct 18 '25 10:10

Salman A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!