Standard JQUERY method to store the original value of a form field?

Is there a standard way in JQUERY (when preparing the document) to store the original values ​​of the form fields on the page to check (later) if the fields were actually changed or not?

I will usually do something like this:

var NameField = $("INPUT[name='NameField']");
//Record the original value.
NameField.OriginalVal = NameField.val();

      

It's simple enough, but I'm wondering if there is a "standard" way to do this.

EDIT

One additional requirement I forgot is that it should work for all types of form fields, including select and textareas.

+2


a source to share


3 answers


The default value (that is, the value specified in the source code using the attribute value

) is automatically available as a property defaultValue

.

alert($("#myInput").defaultValue);

      



More info at W3Schools

It seems to be part of the JS specification since 1.0, so it's safe to use. ( Link , German only sorry)

+13


a source


There is no standard way to store raw values.

jQuery has a better way to store values ​​than your example:



var NameField = $("INPUT[name='NameField']");
NameField.data('OriginalVal', NameField.val());

      

+1


a source


Another easy way is to set a boolean variable when you enter an exchange event control.

i.e. var isFormValueChanged = false;

$("INPUT[name='NameField']").bind("change", function()
{
       isFormValueCanged = true;
});

      

0


a source







All Articles