views:

32

answers:

2

I have a large form with some fields values dependent on previous elements. I use jquery's .trigger event to trigger the dependent field's update functions. When I refresh the page (click reload or click back), the previous values selected are still there, but the dependent fields are not reflecting the other element's values. How can I trigger the update functions upon refresh? I saw a way to prevent the browser from using the form's cached values. I'd rather use the cached values and update the elements dependent on the elements with cached values.

Edit: To illustrate the form:

<form>
    <input id="element1" />
    <input id="element2_dependent_on_element1" />
</form>

element1 remains correct, but element2_dependent_on_element1 .change event is not triggered.

A: 

Why don't you do .trigger on all of the full inputs at the end of document.ready? That should fire all of them off when the page reloads.

ScottyUCSD
I had tried that, but I will try it again and see if it works. Thanks.
Eric Cope
I tried that. I think (not confirmed) that the .trigger fired before the browser applied the browser cache. I ended up doing a .reset on the form after the dom loaded.
Eric Cope
A: 

You can either call form.reset() (DOM function, not a jQuery one) or, trigger the event handlers you're binding immediately after attaching them, for example:

$(function() {
  $("input").change(function() {
    //do something....
  }).change(); //trigger the handler once on load
});
Nick Craver