Issue with jQuery's .show method not functioning properly following the .hide method

Is there a way to make it so that only the "pastInvestments" are displayed when the "pastButton" is clicked, and only the "currentInvestments" are displayed when the "currentButton" is clicked?

In the HTML code below, there are 2 buttons and 2 divs:

<button class="w3-button currentButton">Current</button>
<button class="w3-button pastButton">Past</button>

<div class="currentInvestments">
   //images inside div
</div>

<div class="pastInvestments">
   //images inside div
</div>

By default, I want the "pastInvestments" div to be hidden when the page loads. When the "pastButton" is clicked, only the images in the "pastInvestments" div should be displayed. Currently, clicking on the "pastButton" hides both the "pastInvestments" and "currentInvestments" divs.

Below is the jQuery code I am using:

$(function() {
    $('.pastInvestments').hide();

    $('.currentButton').click(function(){  
      $('.currentInvestments').show();
      $('.pastInvestments').hide();
    });

    $('.pastButton').click(function(){  
      $('.currentInvestments').hide();      
      $('.pastInvestments').show();
    });
});

Answer №1

To ensure the .pastInvestments element remains hidden during page load, you can use CSS to hide it:

.pastInvestments {
display:none;
}

By employing this method, the element will be hidden from the start instead of being hidden after the page has loaded.

This approach has been effective in my experience.

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 are some creative ways to design drop-down menus within an email?

The snippet of code I am currently using lacks the ability to format text, and it does not allow content below dropdown items to shift down properly in an email. <select> <option value="first">First-time users of the Vanguard Events app:&l ...

Having trouble understanding Responsive Frameworks such as Bootstrap?

Looking to dive into the world of responsive frameworks, but feeling overwhelmed by options like Bootstrap, Foundation, Gumby, UI Kit, and Semantic UI? You're not alone. With so many choices and comparisons out there, it's easy to get lost in the ...

Cascading Style Sheets variable and Sassy CSS mixin

I'm facing a challenge involving the use of CSS variables in conjunction with my VueJs app. The goal is to make a hover effect (changing background-color) customizable by the application, while having a default value set in a nested SCSS map using the ...

Issue with authentication in POST request

My current challenge involves attempting a POST request with basic authentication to retrieve an access token from an API. The request requires me to include the grant_type and a code in the body of the request, as well as basic auth credentials (username ...

How to choose a value from an "Input" field

The following input code snippet demonstrates a button with custom attributes: <input type="button" value="O" name="<?php echo $v[$i]['t']; ?>" onclick="javascript:test_parent();"> If there are multiple buttons with unique attribute ...

What does "SPAN CLASS="SKYPE_C2C_FREE_TEXT_SPAN" refer to?

While browsing a website, I noticed the use of this HTML class surrounding customer address information entered in a form. I am curious about the purpose of these tags - perhaps they trigger a specific behavior on Skype? Unfortunately, I couldn't find ...

The image fails to display when using THREE.js and Panolens.js

Trying to create a 360-degree environment with informational buttons using THREE.js and Panolens.JS However, I'm unable to resolve why the image is not appearing. Continuously encountering the error: Uncaught ReferenceError: process is not defined. ...

Tips for utilizing the onload attribute alongside the ng-controller directive to run a function that has been established within the controller

When trying to call the submit() function as soon as the page loads, I keep encountering a submit() method not found error. The positioning of ng-controller and onload is confusing to me. If there is an alternate method in Angular to achieve this, please ...

The function of the Angular ng-include form action for posting a URL appears to be malfunctioning

When a form post is included in a parent HTML using ng-include, it does not trigger the submission action. <div ng-include src="'form.html'"></div> The code in the form.html file is as follows: <form action="next/login" method = ...

Utilizing Vue.js to incorporate the isActive property to the class name within a v-for loop, along with implementing dynamic class names

I am currently using a loop to iterate through some data in my store.js file, and everything is functioning as expected. However, I would like to add a condition to check if the Vue instance is active before applying a specific class to the polygon element ...

Incorporating Bootstrap into a fresh project

I'm completely new to the world of development, so I'll do my best to phrase this question correctly! Last month, I successfully created my first web application using RoR and Bootstrap for some visual enhancements. Currently, I am working on a ...

Reacting like sticky bottoms and tops

I'm working on a react/tailwind project that involves a component I want to be fixed at both the top and bottom of the screen. In simpler terms, there's an element that I need to always stay visible even when the user scrolls up or down the page ...

Invoke a JavaScript function once the div has finished loading

After clicking on a radio button, I am dynamically loading a div element. I want to hide a specific part of the div once it is loaded. $(function($) { $('.div_element').on('load', function() { $('.textbox').hide(); } ...

The server's response contained a MIME type of "application/octet-stream" that did not include JavaScript. Module scripts in HTML are subject to strict MIME type checking

Here is the structure of my project: node_modules server.js public: glsl: fragment.glsl vertex.glsl index.html main.js img.jpg style.css I have set up a simple server to serve a three.js animation in server.js const express = require('expre ...

What are the benefits of incorporating CSS into a CSS block rather than utilizing inline output with HtmlHelper in CakePHP?

Just a few days ago, I embarked on the journey of learning CakePHP through their blog tutorial. Now, I am diving into writing my own small project to gain hands-on experience with the framework. After going through their documentation, I discovered two ...

Encountering an Issue with Incorporating Multiple SCSS files in Jekyll

Currently, I am in the process of developing a website using Jekyll. My goal is to incorporate multiple SCSS files into the project. Although I successfully added a main file, I have encountered some difficulties with additional files. Here is my director ...

The jQuery ajax function delivers responses throughout the entire webpage

I am struggling to make my AJAX return only to a specific DIV on the page, instead of displaying it everywhere. Below is my PHP code in file_ajax.php: <?php require_once "../../funct/cfg.php"; if(isset($_POST['id'])){ if(fileDB($_POST[' ...

Getting to grips with accessing HTML elements within a form

<form name="vesselForm" novalidate> <input type="text" id="owner" name="ownerEdit" required ng-blur="vesselForm.btnCC.attr('value', 'Change Customer')"/> <input type="button" name="btnCC" value="Customer" /> </fo ...

Customize tab background color in Material-UI by utilizing a styledTab component with a passed prop

I've customized this tab to have my desired style: import { withStyles } from "@material-ui/core/styles"; const StyledTab = withStyles((theme) => ({ root: { backgroundColor: "yellow", }, }))((props) => { const { shouldSetBackgroundCol ...

Creating HTML elements using Vue.js

Currently, I am facing an issue while attempting to render a template using the push mutation method. My goal is to push a section component, but instead of seeing the desired template content on my page, I am only getting the raw output of <vsection> ...