Direct jQuery to redirect to a new page and reveal a hidden div

I am facing a situation where I have two web pages named index and results. The results.html page contains some hidden divs with the CSS property set to display: none. My goal is to navigate from the index page using a navigation menu and open the results page while displaying the previously hidden div. The jQuery implementation works perfectly on the results.html page, but I am unsure how to trigger this function from the index page.

Index Page Navigation Menu:

<ul>
   <li><a id="menu-2016" href="results.html#2016">2016</a></li>
   <li><a id="menu-2015" href="results.html#2015">2015</a></li>
</ul>

Results Page Content:

<section id="2016">Content for 2016</section>
<section id="2015">Content for 2015</section>

Associated CSS Styling:

#section-2015, #section-2016 {
    display: none;
}

jQuery Implementation:

$(document).ready(function () {
    $("#menu-2016").click(function () {
        $("#section-2016").slideDown();
        $("#section-2015").slideUp();
    });
    $("#menu-2015").click(function () {
        $("#section-2015").slideDown();
        $("#section-2014").slideUp();
    });
});

Answer №1

To easily verify the URL hash/fragment, you can use the following code snippet.

$(document).ready(function() {
    if (window.location.hash == '#currentYear') {
       $("#section-currentYear").slideDown();
       $("#section-previousYear").hide();
    }
    if (window.location.hash == '#previousYear') {
       $("#section-currentYear").hide();
       $("#section-previousYear").slideDown();
    }
});

Answer №2

One way to achieve this is by utilizing the

window.name

attribute of the window object in JavaScript. This attribute remains consistent even when new documents are loaded in the same window. You can set the window.name to store the page you have navigated from. This can be done on the page load event of your index file. Then, in your result file, you can check this condition on page load and display any hidden content based on the stored information.

Answer №3

To ensure proper functionality, make sure to pass your parameters as actual parameters:

 <ul>
     <li><a id="menu-2018" href="results.html?year=2018">2018</a></li>
     <li><a id="menu-2017" href="results.html?year=2017">2017</a></li>
 </ul>

To extract and process the parameters, refer to this link: How to handle query string in JavaScript?

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

What is the best way to show the current value of a checkbox element in the future?

I want to add multiple checkboxes using this method: $(".btn-sample").on("click", function() { $(".container").append( '<input class="check-sample" type="checkbox" value="'+check_value+'"/>' ); }); The issue I' ...

What is the best way to pass WordPress category as information to ajax?

I am looking for a way to pass the current category of the page to an ajax function. My website is built on WordPress and I need to send the current page's category to infi.php, but I'm not sure how to accomplish this. Here is my ajax code: $.a ...

Is it possible to display a variety of color schemes in just one console.log()?

My task involves working with an array of hexadecimal values, "colors": ["#d5dd90","#e6bb45","#ef9770"] To log these out in different colors, I used the following method: colors.forEach((value)=>{ console.log(& ...

Watching a video play within a slider and transitioning seamlessly to an image once the video ends

I am currently facing an issue with a video playing inside a HeroCarousel slider. Before the slider, there is an image and after the slider, there is another image. This is my code: <div data-time="8000" data-prev-src="/media/splash/slider-left.png" ...

how can I insert an asp textbox into a div using jquery?

I have an asp.net textbox (<asp:TextBox></asp:TextBox>), and I would like it so that when I click a button, the textbox is placed inside a div like this output (<div><asp:TextBox></asp:TextBox></div>). I tried using the ...

Switch out either even or odd divs within a container

I need help figuring out how to alternate the background colors of divs within a container. I want the first one to have a red background, the second to have blue, the third red again, and so on... Here is the code I currently have: <div>...</di ...

What strategies can I use to streamline this array update function code?

Looking to simplify my updated array function. The update function involves updating and comparing values in an array. The comparison will be done within the fruit_temp. For example, data in fruit_temp's fruit_db_id corresponds to an existing id in th ...

How to perfectly align squares in CSS Grid

I'm working on arranging four squares in a grid pattern, two on top and two on the bottom, with equal spacing between them. Currently, the squares shift based on the fr value in CSS grid, resulting in uneven spacing like this: I want to align all fou ...

Tips for submitting a form remotely using Radix Alert Dialog and the form attribute

Currently, I have integrated a Radix UI's Alert Dialog in my Next.js 13 app to confirm the deletion of a contact from the user's contact list: Take a look at the Alert Dialog modal here However, there seems to be an issue with the delete button ...

Display a message indicating no data is available if the specified text is not found within the div

In the code snippet below, there is an input element followed by a div containing multiple child elements: <input type="text" onkeyup="filter()" id="filter_data"> <div id="body"> <div class="child"> Text 1 </div> <div class ...

CSS for leaving white space at the end of one third of a container

Currently developing a website and facing an issue with the layout: I am trying to create 3 columns of equal height with each column taking up one-third of the width. However, there seems to be a small white gap on the right side of the last column. Here ...

The functionality of the ui sortable feature in Angular is not effective when used on a single-page website

My latest project involves using a single-page application. To connect the UI Angular library, I followed these steps: I started by adding the necessary scripts: <script src=.....jquery-1.9.1.js"></script> <script src=.....jquery-ui.js"> ...

Exploring the wonders of jQuery Ajax within Yii

I am developing a custom shop application and I have a query. In my project, the implementation of Ajax in Yii involves: <?php echo CHtml::ajaxLink( '', array("cart/add/id/$item->id"), array( ' ...

use ajax to dynamically append a dropdown menu

Currently working on creating a form that includes a dropdown menu populated with elements from a database. The challenge I'm facing is ensuring that once an element is selected from the dropdown, another one appears dynamically. My goal is to use AJA ...

Styling and Div Container management with Bootstrap in ASP.NET Code Behind

I am currently in the process of developing an ASP.NET application that requires the dynamic creation of "TextBox" and "DropdownList" controls. Here is the method I have been using to create "TextBox" controls: private void CreateTextBox(string id, string ...

What is the best way to toggle the visibility of the answers to a question when the corresponding hyperlink is clicked, and how can the text on this hyperlink be updated dynamically using jQuery?

I am currently working on a website using PHP, Smarty, and jQuery. On this website, I have a page that features a list of questions along with their available answer options and the correct answer. This page is dynamically generated using the "Smarty Tem ...

Utilizing a box within a background image using HTML and CSS

In the midst of my college project, I've encountered a hurdle. There's an image I need to overlay with a box that has padding on all sides corresponding to the outline of the image. Despite my attempts, I haven't been successful in achieving ...

Symfony 2 lacks the ability to automatically create the web/bundle/framework structure

I encountered a major issue with Symfony. After installing Symfony 2.7.5 via the command line: $ symfony new my_project The problem arose in the generated project directory: /web/bundle In this folder, I found two empty files (not directories!) named fr ...

Does renaming a sub-directory in the static directory of an IntelliJ Spring Boot project cause any changes?

I'm a newcomer to IntelliJ and I've been using it to develop a Spring Boot application. Right now, my project structure looks like this: https://i.sstatic.net/7A7jb.png My goal is to set up a "css" directory within the "static" directory under ...

Problem encountered with cross-domain jQuery.ajax request, although it functions properly in the outdated iteration

Here is the code snippet I am using: $.ajax({ url: "http://example.com/auth", type: "POST", data: "username=example&password=mypasswd", success: function(returned) { // do something }, error: function(returned) { ...