Solved

javascript not refilling after going back

Posted on 2013-12-30
8
349 Views
Last Modified: 2014-01-02
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?
0
Comment
Question by:rivkamak
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39747626
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
0
 
LVL 9

Accepted Solution

by:
Sar1973 earned 250 total points
ID: 39747925
You could add an onload event that charges the data as the populating function does.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39748012
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.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:rivkamak
ID: 39748602
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?
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39749561
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

0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 250 total points
ID: 39749805
Just trigger the change event onload

$(function() {
  $('#make').change();
});
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39749970
That's what I've argued on post 39747925...
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39749998
You argue to re-execute the function. It is much simpler, just trigger the change event on the select
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Converting dates in JavaScript 2 32
Increase the size of the modal in Bootstrap 5 24
div to fit another div 8 21
Which non-HTML GUI front end to use with Java? 3 21
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question