[jQuery] Re: Resetting the reset button

2009-05-06 Thread Steven Yang
i dont think you can do thatyou probably have to cache your state yourself
i think reset uses the defaultValue attribute of the input and i think the
attribute is readonly.

correct me if i am wrong


[jQuery] Re: Resetting the reset button

2009-05-06 Thread RobG



On May 1, 1:49 am, russellneufeld russellneuf...@gmail.com wrote:
 Hi all,

   I've got a set of forms which all act the same way - the form submit
 is handled by the jQuery form plugin which redirects the output to a
 div on the page.  That means that when the user hits submit, the page
 in the browser doesn't change.  The success or failure of the form
 submit is displayed on the existing page.

   The pages all have a submit and a reset button.  The issue is
 that the reset button holds the state of the form when the page was
 originally sent to the browser.

Only if you do that yourself, normally the reset button effectively
does nothing more than call the reset method of the form.

 That means that after someone changes
 the form and submits it successfully, the reset button returns the
 form to the previous state when the page was originally sent to the
 browser.  But that state doesn't really reflect the current state of
 the form variables (as held on the server) since an update has been
 made.

Form controls have either a defaultValue, defaultChecked or
defaultSelected property, depending on the type of element, that holds
the default value of the control.  When the form is reset, the
control's value is reset to the relevant default.


   My question is this - is there a way to update the reset button so
 that it resets the form to a new state, one that might have changed
 since the browser originally rendered the page?

Change the default[Value|Checked|Selected] property to whatever you
want, then the reset button will reset to that value.

URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-26091157 
URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-37770574 


--
Rob


[jQuery] Re: Resetting the reset button

2009-05-06 Thread russellneufeld

Rob,

  Thanks a ton.  I was able to code up what I needed with your
advice.  Here's what I did, in case someone else out there needs to do
something like this in the future.

Russ

/**
 * Set all the default values of a form to whatever is there now,
effectively updating the reset
 * button to restore the form to the current state (which might be
different than the state the
 * form was in when the page was loaded).
 *
 * @param formID  Form id (starting with #) to update the default
values of
 */
function resetFormToNewValues(formID)
{
$(formID).find(':input').each(function ()
{
if ($(this).is(':text'))
{
// TODO - Not tested for textareas
$(this).attr(defaultValue, $(this).attr(value));
}
else if ($(this).is(':checkbox, :radio'))
{
$(this).attr(defaultChecked, $(this).attr(checked));
}
else if ($(this).is('select'))
{
$(this).find(option:selected  ).attr
(defaultSelected, true);
$(this).find(option:not(:selected)).attr
(defaultSelected, false);
}
else if ($(this).is(':password, :file'))
{
// TODO - This is untested
$(this).attr(defaultValue, );
}
});
}


[jQuery] Re: Resetting the reset button

2009-05-05 Thread russellneufeld

Hi all,

  Was hoping to hear an answer to this question, but perhaps I didn't
explain it clearly enough.  Is there a way to update a form such that
a reset button restores to the new state?  I'd like to make the reset
button restore a form to a state which is newer than the one when the
page was loaded.

  Thanks,

Russ