Avatar of rivkamak
rivkamak
Flag for United States of America asked on

javascript not refilling after going back

I have a form that select box #2 gets populated after select #1 is picked.
then they click submit and get brought to a longer page.
If someone uses the back button on their browser, the box #1 is still selected, but my javascript populated second box is now empty.
How can I set it up ,if someone just hits back that second box will populate with the first box information?
jQueryJavaScriptHTML

Avatar of undefined
Last Comment
Michel Plungjan

8/22/2022 - Mon
Rob

It would all depend on how you handle your state.  Ideally this is done server side via sessions. What are you using server side?
To just suggest an answer based on this one example will not work if the Javascript isn't specific to the page. Eg, when the page loads you could test to see if the first drop down has a selected value and call the same function that is called with the Onchange event. But this could cause issues if you re use the Javascript code on other pages. Very hard to say without seeing how and what you've done to code this
ASKER CERTIFIED SOLUTION
Sar1973

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Rob

That's right, exactly what I said

when the page loads you could test to see if the first drop down has a selected value and call the same function that is called with the Onchange event.

However, what works for one page may not work with the next.
rivkamak

ASKER
I don't want the onchange event to run again, because it just loads 10 items in a select box.
I need to know which item was selected.

I tried putting this code
 <script>
      $( document ).ready(function() {
if ($('#make').val != "") {
$('#model').val(<%=session("usermodel")%>);
}
});
</script>

Open in new window

I set that session on the second page.
When you click back though the page doesn't populate again , it doesn't fully reload, so it doesn't recognize that session variable.

Is there any way to get what the selected index would have been on that second box?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Rob

Val us a function not a property so you'll need the brackets (). Also missing were the inverted commas around the session variable to indicate a string and not a variable
<script>
      $( document ).ready(function() {
if ($('#make').val() != "") {
$('#model').val('<%=session("usermodel")%>');
}
});
</script>

Open in new window

SOLUTION
Michel Plungjan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Sar1973

That's what I've argued on post 39747925...
Michel Plungjan

You argue to re-execute the function. It is much simpler, just trigger the change event on the select
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.