Ways to conceal and reveal content within div elements

I have a code snippet that is currently operational, but I am looking to enhance it by displaying one div at a time.

If you would like to view the code, please visit my CodePen link below:

https://codepen.io/danongu/pen/YzXvpoJ

Due to the extensive amount of HTML and CSS in the code, I refrained from sharing the entire content. My primary issue lies with the JavaScript portion.

My objective is as follows:

Upon clicking the 'Section A Book List' button, only the 'PREVIOUS VERSIONS A' and 'NEW VERSIONS A' sections should be visible.

Similarly, when selecting the 'Section B Book List' button, only the 'PREVIOUS VERSIONS B' and 'NEW VERSIONS B' sections should appear.

Regarding the black areas for 'Book 1' and 'Book 2,' I aim to display 'BOOK 1' when 'Book 1' is clicked, and 'BOOK 2' when 'Book 2' is selected.

Essentially, I require some form of conditional show/hide functionality. I attempted to implement this using jQuery in the provided CodePen example, but I encountered difficulties progressing beyond that point.

Your insights are greatly appreciated. While I made an initial effort, I reached an impasse during the process.

Answer №1

To make each section of the book list wrapper unique, you can assign a specific class or id to them. Here's an example:

<div id="bbl-wrapper1">
  <button class="toggle1">Section A Book List </button>
  <button class="toggle2">Section B Book List </button>

  <div class="clearfix" id="section-a">...</div>

  <div class="clearfix" id="section-b">...</div>
</div>

By default, you can hide #section-b:

#section-b {
  display: none;
}

You can use jQuery to control the visibility of each section like this:

$(document).ready(function() {
  var $secA = $("#section-a");
  var $secB = $("#section-b");

  $('.toggle1').click(function() {
    $secA.show();
    $secB.hide();
  });

  $('.toggle2').click(function() {
    $secA.hide();
    $secB.show();
  });
});

The same approach can be applied to manage BOOK 1 and BOOK 2 sections as well.

Note: Avoid using duplicate ids like bbl in your HTML as they must be unique for each element.

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

Determining the width of a window using JavaScript

My website is experiencing some mysterious issues with $(window).width(). When I open my site in the Chrome Device Toolbar with a window size of 320xXXX, then run $(window).width() in Google Chrome's JavaScript console, it returns 980. As a result, n ...

The Wordpress admin-ajax.php script is failing to process the function and returning a "0" error code

I have been experimenting with processing AJAX requests in Wordpress and I'm following a particular tutorial to achieve this. The goal is to create a basic AJAX request that will display the post ID on the page when a link is clicked. The Approach ...

Choose the element that is trapped within the div

I've hit a roadblock with this issue that I just can't seem to figure out. I was working on a practice project creating a Netflix-like webpage, and the select element in the footer containing languages is stuck and won't budge. Here's m ...

Displaying cards horizontally on desktop and vertically on mobile using CSS

Context and Issue I am currently working on this specific page, with a focus on the "Top Artists" section. Desired Outcome: When viewed from a laptop, I aim for the cards to be displayed as vertical rectangles, where the ranking is at the top, followe ...

"Utilizing jQuery AJAX to enable multiple file uploads with specific file extensions

I've been facing an issue with uploading multiple files using jQuery and AJAX. The problem arises when I attempt to submit a PNG or JPG file, but it fails to submit and instead alerts me to "Please Upload File" even though a file has been selected. ...

Deciphering JSON File Paths

I'm having trouble finding the correct path to a local file. My directory structure is as follows: Resources -> data -> file.json js -> folder -> script.js html -> folder -> file1.html I ...

How can a callback be properly passed in programming?

My coding approach is outlined below: var CustomLibrary = (function (window, $, undefined) { return { URI: 'http://testpage/API/', OnSuccess: function (data, status) { }, OnError: function (request, status, error) { } ...

Executing jQuery AJAX request twice

Every time the ajax function is called, it seems to be uploading multiple images. For example, on the first call it works fine, but on the second call it uploads two times the number of images from the previous call, and this pattern continues with each ...

Creating a dropdown menu in Bootstrap 4 using JSON information

I am trying to create a dynamic drop-down menu using an input field with a drop-down button inside a form. Currently, I am attempting to populate the drop-down menu with static JSON data. However, I am encountering issues with getting it to function proper ...

Ways to make a component gradually appear and disappear in an Angular application

I have developed a task management application using Angular and I wanted to implement a fading effect for each task when it is created and before it is deleted. Despite successfully applying the fade in effect at the todo-item component level, I encounter ...

Customizing the Twitter bootstrap navigation in Wordpress

I've been working on theming my navigation menu using Twitter Bootstrap. I want it to resemble the demo example of a fixed top banner, which can be seen here: http://getbootstrap.com/examples/navbar-fixed-top/ Although I have managed to get it to wor ...

Update the div element without needing to reload the entire page

Is there a way to reload a div tag without refreshing the entire page? I understand this question has been asked before, but I want to make sure I have a clear understanding. <p>click HERE</p> <div class="sample"> <?php functi ...

The request to access /test.php has resulted in an error (404) indicating that the file cannot be found

Disclaimer: I am completely new to php. I recently followed a tutorial on creating a php script that captures input from a sign-up page (html) and saves it in a database (mysql/Wamp). However, when I attempted to test the functionality, I encountered the ...

What is the reason for the successful addition of a list item, using dynamic methods, in Chrome and Firefox while encountering issues in

I have encountered an issue with my jQuery code that works perfectly fine in Firefox and Chrome but fails to work in Internet Explorer 8 (I haven't tested it on other versions of IE). It is important to note that Internet Explorer is running in standa ...

php code to show data in a single column of a table

I am working with a database record that includes an id and name, and I need to display it in a table where records are distributed in the pattern 1-2-1-2-4 across columns. Below is my current code that is generating the incorrect output: <table borde ...

Field for user input featuring a button to remove the entry

I am attempting to add a close icon to a bootstrap 3 input field, positioned on the top right of the input. Here is what I have tried so far: https://jsfiddle.net/8konLjur/ However, there are two issues with this approach: The placement of the × ...

How can I prevent StateHasChanged() from affecting all open browsers in a Blazor Server-Side application?

Hey there, I'm new to Blazor but familiar with C#. This is my first time asking a question here so please bear with me. :) I am working on a Blazor server-side application where users can enter information and validate it based on server data. So far ...

Centering all td elements except for those with a specific class

Consider the following code snippets: <tr> <td class="foo">chuchu</td> <td>chuchu</td> <td>chuchu</td> <td>chuchu</td> <td>chuchu</td> </tr> Is there a way to center align thes ...

The `background-size` property in jQuery is not functioning as expected

I am facing the following issue: When I click on a div with absolute positioning, I want to animate its position, width, and height. In addition, I want to change the background-size using jQuery. However, the problem is that all CSS properties are updat ...

What is the best way to refresh a page during an ajax call while also resetting all form fields?

Each time an ajax request is made, the page should refresh without clearing all form fields upon loading Custom Form <form method='post'> <input type='text' placeholder='product'/> <input type='number&a ...