Switch between various div elements

Looking for a way to toggle between different divs? Here's the scenario: You have a sidebar with a set of buttons. Upon entering the page, you only want the div containing the button group to be visible. However, when a specific button (e.g. Info) is clicked, you want the corresponding div (info) to appear while the others disappear. And if the back button on the Info div is pressed, you want to revert back to the initial state with only the button group visible.

.info {
  border-color: black;
  background-color: #f1f1f1;
  margin-left: 7%;
  margin-right: 7%;
  height: 60%;
  position: relative;
}
.about {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 <body>
    <div class="sidebar">
      <h3>Metadata & additional services</h3>
      <div class="button-group" id="showall">
        <button class="activeButton" id="info-button">Info</button>
        <button class="activeButton">Quotes</button>
        <button class="activeButton">Additional</button>
        <button class="activeButton">Additional</button>
      </div>
      <div class="info" id="info">
        <h3>Info</h3>
        <ul>
          <li>Author:</li>
          <li>Title:</li>
          <li>Content:</li>
        </ul>
        <button class="back-button">Back</button>
      </div>
      <div class="about" id="about">
        <h1>about</h1>
      </div>
    </div>

    <script>
      $(document).ready(function () {
        $("#info-button").click(function () {
          $("showall").hide();
          $("#info").show();
        });
      });
    </script>
  </body>

Answer №1

If you want the info div to be hidden on page load, ensure that the display property is set to none. Additionally, make sure to include the id identifier in your .click function and add another event handler for the #backBtn.

$(document).ready(function () {
        $("#info-button").click(function () {
          $("#showall").hide();
          $("#info").show();
        });
        $("#backBtn").click(function () {
          $("#showall").show();
          $("#info").hide();
        });
      });
.info {
  display: none;
  border-color: black;
  background-color: #f1f1f1;
  margin-left: 7%;
  margin-right: 7%;
  height: 60%;
  position: relative;
}
.about {
  display: none;
}
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" referrerpolicy="no-referrer"></script>
</head>
   <body>
    <div class="sidebar">
      <h3>Metadata &amp; additional services</h3>
      <div class="button-group" id="showall">
        <button class="activeButton" id="info-button">Info</button>
        <button class="activeButton">Quotes</button>
        <button class="activeButton">Additional</button>
        <button class="activeButton">Additional</button>
      </div>
      <div class="info" id="info">
        <h3>Info</h3>
        <ul>
          <li>Author:</li>
          <li>Title:</li>
          <li>Content:</li>
        </ul>
        <button class="back-button" id="backBtn">Back</button>
      </div>
      <div class="about" id="about">
        <h1>about</h1>
      </div>
    </div>

    <script>
      $(document).ready(function () {
        $("#info-button").click(function () {
          $("showall").hide();
          $("#info").show();
        });
      });
    </script>
  </body>

</html>

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

Troubleshooting Problem with Express Server Routing

I'm currently facing an issue with my error handling while trying to implement a new route to retrieve a user by their id. Below is the code snippet for this specific route. const express = require('express'); require('./db/mongoose&a ...

There seems to be an issue with the zebra striping in the rich:datatable component when using the rowClasses

I'm attempting to give my table a "zebra" color pattern for the rows, but it seems like the rowClasses attribute isn't functioning properly (edit: I don't see any colors applied to the table at all). Here is how I defined the styles: <s ...

How is it possible to encounter a missing semicolon CssSyntaxError during a Gatsby build?

Currently, I am in the process of developing a Gatsby page with Material UI. The design of the page is almost finalized; however, upon completing the project, an unexpected build error occurs when running npm run build. WebpackError: Pathname: /invitation/ ...

Refresh the information displayed in the HTML table

I've been assigned the task of developing a shift management web app to help our current employees track their shifts. The website StaffHub has been suggested as a reference for my web app, and I am currently focused on implementing the calendar featu ...

AJAX: Unable to retrieve an Associative array from the database

In my HTML form, I have two combo boxes that need to be dynamically populated from a database using jQuery and PHP. Whenever one of the combo boxes is changed, I trigger a function in jQuery which sends the selected value to a PHP page to fetch data from t ...

Angular's UI Modal: utilizing inline template and controller functionality

I am looking to create a simple confirmation box using UI-modal, which I have used successfully for more complex modals in the past that load their template and controller from external files. However, this time I want something even simpler - just a basi ...

Extracting information from a Weather API and sharing it on Twitter

Can anyone help me troubleshoot my Twitter bot setup for tweeting out city temperatures? I attempted switching to a different API, but nothing seems to be resolving the issue. console.log('initiating twitter bot...') var Twit = require('t ...

Updating to the latest version of Next.js will result in a modification of the API path

I recently updated my app to the latest version of nextjs (13.1.6) and encountered a problem where all my requests are now failing. It seems that the URL I specified in the request creator, based on the ENV value, is being overwritten. .env file CONST NEXT ...

Ensure the date is displayed in the format of dd-mm-yyyy when using the input type=date

Here is the code I am currently using : <input type="date" class="form-control" id="training_date" name="training_date" placeholder=" select" value="" onfocus="(this.type='date')" onfocusout="(this.type='date')" max=<?php echo ...

What is the process for writing code in a recursive manner?

I'm in the process of converting these code snippets into smaller ones using recursion. However, I've hit a roadblock while trying to implement a for loop. The dictionary I am working with is: var structure = []; and it has the following structu ...

ReactJS attempting to invoke a class function using a dynamically generated button

When attempting to access the deletePost(index) method from the ShowPost class using a dynamically rendered button within the render() step in React, I encounter an issue. The button labeled "click me" successfully retrieves and prints the first item in my ...

Utilizing JavaScript within Live Cycle Designer

Looking for a solution to streamline the process of selecting TV channels for sales reps. We have around 150 options, but the reps only need to choose from a list of 50. The selected channels must then be displayed on another page in alphabetical order. ...

Can a div with float: left; be centered?

Currently, I am attempting to create a dock (similar to the iOS dock) for my website. Below is the full code that I have implemented: function addPrevClass(e) { var target = e.target; if (target.getAttribute('src')) { // check if it is img ...

Using Node.js to read and replicate a JSON file

As someone who is relatively new to the world of NODE.JS, I am eager to level up my skills. Currently, I have a NODE.JS script that gathers data from a feed, resulting in a large JSON file. This file is then used by webpages and mobile apps to display con ...

GATSBY: Error: Unable to find the specified property 'includes' within an undefined value

I'm struggling to figure out how to properly filter images in my portfolio website as discussed in this post… Every time I attempt it, I encounter the following error: "TypeError: Cannot read property 'includes' of undefined" D ...

Calling a function within another function

In my code, I have a function that formats the price and retrieves the value needed for refactoring after upgrading our dependencies. I'm struggling with passing the form value to the amountOnBlur function because the blur function in the dependencie ...

What is the best way to display the legend on my PieChart?

I am attempting to display the names of colors in my PieChart as a legend. Here is the JSON data: { "choice": "20-29 yrs. old", "count": 4 }, { "choice": "30-39 yrs. old", "count": 2 }, { "ch ...

Managing error responses while fetching a blob through AJAX

Is it possible to execute an AJAX call that has the potential to return either a blob or a text string based on the server's response? My current task involves using AJAX to transform a user-provided video into an audio blob that can be utilized with ...

Issues with jQuery Ajax functionality in Rails application not achieving successful completion

When a card is moved onto another stack, an Ajax call is triggered twice. This only happens when there are multiple stacks. However, if the cards are rearranged within the same stack, the call is triggered only once. The Ajax call sends an array of IDs fo ...

How can I pass function arguments dynamically to a nested function in Node.js?

Currently using Node 6.11.0, I am attempting to accomplish the following dynamically: const parentFunc = (arg1, arg2, arg3, arg4) => { childFunc('foo', arg1, arg2, arg3, arg4); }; I have attempted this method (without success): const pare ...