Using the back button in the browser can undo any CSS modifications made by JavaScript

I'm currently working on a form that changes the displayed select lists based on the selection of a previous list.

HTML

<select name="college" id="college" onchange="show_majors()">
                <!-- <option value="-">-</option> -->
                <option value="COE">College of Engineering</option>
                <option value="CBE">College of Business and Economics</option>
                <option value="CS">College of Science</option>
                <option value="CHSS">College of Humanities and Social Sciences</option>
                <option value="CIT">College of Information Technology</option>
                <option value="CEDU">College of Education</option>
                <option value="CL">College of Law</option>
                <option value="CFA">College of Food and Agriculture</option>
</select>

        <div id="COE_MAJORS" class="majors">
            <p><strong>Major:</strong></p>
                <select name="COE_major" id="COE_major" onchange="show_clusters()">
                    <option value="OTHER">-</option>
                </select>
        </div>
        <div id="CBE_MAJORS" class="majors">
            <p><strong>Major:</strong></p>
                <select name="CBE_major" id="CBE_major" onchange="show_clusters()">
                    <option value="OTHER">-</option>
                </select>
        </div>
        .....

CSS

.majors {
   height: 0px;
   overflow: hidden;
}

Javascript

<script type="text/javascript">
            function show_majors() {
                var college = document.getElementById("college");
                college = college[college.selectedIndex].value;
                var majors = document.getElementsByClassName("majors");
                for(var i = 0; i < majors.length ; i++) {
                    majors[i].style.height = "0px";
                }
                if (college != "-") {
                    document.getElementById(college + "_MAJORS").style.height = "auto";
                }
            }
</script>

The issue arises when a user selects a college and major, then clicks submit (which leads to another page). Upon returning with the back button, the last selected college remains but the majors DIV box is reset. Is there a way to display the corresponding major div box for the college selected when the user returns using the back button, preventing it from resetting?

Answer №1

When the window's load event occurs, you can access the restored selection.

If you have a function in place that modifies the page based on the current selection, the easiest approach is to execute it upon page load:

<script>
addEventListener('load', display_selected_items);
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

access pictures from a different directory using JavaScript

I am working with an images array that contains three jpg files. I am trying to set the background image of a class called pic using images from the array. The issue I'm facing is that the images are stored in an images folder with the URL images/. I ...

Is it feasible to save BoxGeometries or MeshLambertMaterial in an array using Three.js?

var taCubeGeometryArray = new Array(); var taCubeMaterialArray = new Array(); taCubeGeometryArray.push(new THREE.BoxGeometry( .5, .5, .5)); taCubeMaterialArray.push(new THREE.MeshLambertMaterial({map: THREE.ImageUtils.loadTexture( "image.png" )})); Could ...

An error occurs when attempting to use Socket.io without explicitly returning the index.html file

I want to implement WebSockets without needing to return the index.html file. As someone new to Socket.IO, here's what I've attempted: First, I installed Socket.IO using npm: npm install socket.io --save Then, I created a file called index.js ...

Reordering elements in ng-repeat using Angular's orderBy filter for ascending sorting

My JSON data looks like this: "db" : { "x" : { "0" : "A", "1" : "B", "2" : "C", "3" : "D", "4" : "E", "5" : "F", "6" : "G", "7" : "H", "8" : "I", "9" : "J", "10" : ...

Web page layout featuring fields set aside for completion with underlined formatting

As I work on an HTML document, I encounter a challenge with styling fields that need to be dynamically filled and underlined. Here is what I am aiming to achieve: "Applicant's Last Name:_________bar_____________ First Name:_________foo_____________" ...

PHP combined with jQTouch

I have a form on an HTML page that is passed to a PHP script. The PHP works fine, but I am facing some issues in displaying the HTML content on the PHP page. When I use <?php echo $message; ?> in the body of the HTML, the variable displays properly. ...

Navigating a dropdown menu in an Asp.net application: choosing an option

I am currently working on an ASP.net project that incorporates Bootstrap controls. The issue I am facing involves a Bootstrap dropdown menu that dynamically populates its list items from a database. The problem arises when I click on the menu and select a ...

Algorithm for Generating Cubes

My current task involves working with a dynamically generated array of cubes, each having its distinct position and color. These cubes are situated on a 5x5 field, where each cube is connected to at least one other cube. Now, I am looking to introduce a ne ...

Transforming HTML with JavaScript

I am facing a challenge in my JavaScript application where I receive HTML code from an endpoint that contains radio buttons. Unfortunately, I cannot modify the HTML content coming from this endpoint. My goal is to convert these radio buttons into regular b ...

Trouble accessing JSON file on FileZilla/FTP server

When I transfer my project to the FTP server, the JSON file I am using for data is not functioning properly. However, when I run the program on XAMP, my local server, it works perfectly. Upon inspecting the element on the FTP server, I noticed that the JSO ...

Using jQuery to add the input from a dynamically loaded Ajax select list to a different form prior to submission

Let's simplify the code for this question: I have two HTML forms with IDs of 'header' and 'existing' In the 'existing' form, there is a select element where the options list is loaded dynamically using Ajax based on sea ...

Having trouble with the auto-complete feature in the search box when using Jquery

I'm currently working on implementing TypeAhead functionality for a search textbox. Within the form, I have 2 radio buttons and if one of them is selected, I need the type-ahead feature to populate the list of masters in the search box. //html < ...

Anticipated a task or method invocation but encountered an expression instead no-unused-expressions (line 14/15)

I can't seem to figure out the issue with my code. Despite trying to fix it multiple times by adjusting lines 14/15, I keep encountering the same error. If anyone has any insights or suggestions on what might be causing this problem, I would greatly a ...

How can I create a custom checkbox in React Bootstrap without specifying an ID

I'm struggling to understand why this seemingly straightforward Bootstrap custom checkbox isn't functioning as expected. My project involves React, Bootstrap, and React-Bootstrap. import React from "react"; import ReactDOM from "react-dom"; impo ...

Is there a way to customize the color of the highlighted stars in a jQuery rating plugin?

Utilizing a jquery plugin, I have successfully implemented a star rating feature sourced from this location. The stars are appearing as intended, but upon selection, the default color is red. This selected color is specified in the .rateit .rateit-selected ...

Animating the opacity of elements using jQuery and CSS

Trying to put together a fading slideshow with five slides that will loop back to the beginning. My code seems like it should do the trick, but there's something not quite right. <script type="text/javascript"> $(document).ready(function( ...

linking to a page that shows the user's chosen option from a dropdown menu

One of the challenges I encountered was creating a feature that allows users to share a specific selection from multiple dropdown lists on a page. Essentially, my goal was to enable users to send a link that would direct others to the same page with the ex ...

Need help with a countdown function that seems to be stuck in a loop after 12 seconds. Any

I am facing an issue with a PHP page that contains a lot of data and functions, causing it to take around 12 seconds to load whenever I navigate to that specific page. To alert the user about the loading time, I added the following code snippet. However, ...

Invoke a function within the <img> tag to specify the source path

I have been attempting to achieve something similar to the following: <img id="icon" class="cercle icon" src="getIcon({{item.status}})" alt=""> This is my function: getIcon(status){ switch (status) { case 'Ongoing': ret ...

Utilize separate environment variables for distinct environments within a React project

Is there a secure method to externalize all environment variables, including secret keys, for a React application within a DevOps setup? Our goal is to streamline the build process across different environments, each with its own unique set of environment ...