I'm looking for a way, with jQuery, to get the default value of an input field (in an HTML field).
Seems simple enough. Just calling $("#id-of-field").attr('value')
should return the value I'm wanting right?
Thing is, if the value is Foo
, when I load the page, it returns Foo
. No problem there. But if I write Bar
in the field, then reload (not submit) the page, $("#id-of-field").attr('value')
will return me Bar
, even if in the source code, the value attribute of the field is still Foo
.
That makes validation of my form sticky, because when I get my default values to ignore them if the field has not been filled, I could get a real value in the mix of "Values to be ignored".
FYI, I can't put in the values manually, since the form is dynamic, and cannot query directly the database because it's a Wordpress Website.
I'm looking for a way, with jQuery, to get the default value of an input field (in an HTML field).
Seems simple enough. Just calling $("#id-of-field").attr('value')
should return the value I'm wanting right?
Thing is, if the value is Foo
, when I load the page, it returns Foo
. No problem there. But if I write Bar
in the field, then reload (not submit) the page, $("#id-of-field").attr('value')
will return me Bar
, even if in the source code, the value attribute of the field is still Foo
.
That makes validation of my form sticky, because when I get my default values to ignore them if the field has not been filled, I could get a real value in the mix of "Values to be ignored".
FYI, I can't put in the values manually, since the form is dynamic, and cannot query directly the database because it's a Wordpress Website.
Share Improve this question edited Nov 23, 2011 at 19:02 César 10.1k6 gold badges55 silver badges75 bronze badges asked Nov 23, 2011 at 18:44 Fredy31Fredy31 2,8356 gold badges36 silver badges61 bronze badges3 Answers
Reset to default 6Input fields have the defaultValue
DOM property that does exactly what you need.
According to this answer, the jQuery (> 1.6.3) way to access it is
$('#element').prop('defaultValue')
Alternatively, in pure JavaScript, e.g. -
document.getElementById("element").defaultValue
Try this
$('inputSelector').prop('defaultValue');
You could try -
$("#id-of-field")[0].defaultValue
That should get the DOM defaultValue
property which should give you the original value of the textbox
https://developer.mozilla/en/XUL/textbox#p-defaultValue