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.
Input 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
Alternatively, in pure JavaScript, e.g. –
Try this
You could try –
That should get the DOM
defaultValue
property which should give you the original value of the textboxhttps://developer.mozilla.org/en/XUL/textbox#p-defaultValue