Find default value of field after page refresh

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?

Read More

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.

Related posts

Leave a Reply

3 comments

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

    $('#element').prop('defaultValue')
    

    Alternatively, in pure JavaScript, e.g. –

    document.getElementById("element").defaultValue