Find the default value for a field after refreshing a page

I am looking for a way, with jQuery, to get the default value for an input field (in an HTML field).

It seems simple enough. Just calling $("#id-of-field").attr('value') should return the value I want correctly?

The thing is, if the value is Foo , when I load the page, it returns Foo . No problems. But if I write Bar in the field, then reload (do not send) the page, $("#id-of-field").attr('value') will return Bar me, even if the field value attribute is still Foo in the source code .

This makes checking my form sticky, because when I get the default values ​​to ignore them, if the field was not filled, I could get the real value in the "Values ​​to be ignored" mix.

FYI, I cannot manually enter the values, since the form is dynamic and cannot directly query the database, because it is a Wordpress website.

+4
source share
3 answers

Input fields have a defaultValue DOM property that does exactly what you need.

According to this answer, the way to access it is jQuery (> 1.6.3)

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

Alternatively, in pure JavaScript, for example. -

 document.getElementById("element").defaultValue 
+5
source

try it

 $('inputSelector').prop('defaultValue'); 
+1
source

You can try -

 $("#id-of-field")[0].defaultValue 

This should get the DOM defaultValue property, which should give you the original value of the text field

https://developer.mozilla.org/en/XUL/textbox#p-defaultValue

+1
source

Source: https://habr.com/ru/post/1382754/


All Articles