Attempting to implement a single toggle for numerous div elements

Concerning the answer provided in this query: How to slide down a div then .fadeIn() the content and vice versa?

In the js fiddle shared, the jQuery code is specifically designed for one of the divs. If there are multiple divs, how can the jQuery code be adjusted to cater to any clicked div? Despite my attempts to adapt the code, it does not seem to function correctly:

<a href="#" onclick="toggleSlider();">toggle</a>
<div id="panelThatSlides" style="display:none;background:#eee;padding:10px;">
    <div id="content1Fades" style="opacity:0;filter:alpha(opacity=0);">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ut tortor  erat, et consectetur nisl.
    </div>
</div>
<p></p>
<a href="#" onclick="toggleSlider();">toggle2</a>
<div class="panelThatSlides2" style="display:none;background:#eee;padding:10px;">
    <div id="content2Fades" style="opacity:0;filter:alpha(opacity=0);">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ut tortor  erat, et consectetur nisl.
    </div>
</div>
<p>HELLO WORLD</p>

I believe I might be incorrectly referencing the "contentthatfades" section with the jQuery:

function toggleSlider() {
    if ($("#"+ this.id).is(":visible")) {
        $("#content"+this.id+"Fades").animate(
            {
                opacity: "0"
            },
            600,
            function(){
                $("#"+ this.id).slideUp();
            }
        );
    }
    else {
        $("#"+ this.id).slideDown(600, function(){
            $("#content"+this.id+"Fades").animate(
                {
                    opacity: "1"
                },
                600
            );
        });
    }   
}

Answer №1

Revise the HTML code as follows:-

<a href="#" onclick="toggleSlider(this);">toggle</a>
<div id="panelThatSlides" style="display:none;background:#eee;padding:10px;">
<div id="content1Fades" style="opacity:0;filter:alpha(opacity=0);">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ut tortor  erat, et consectetur nisl.
    </div>
 </div>
  <p></p>
   <a href="#" onclick="toggleSlider(this);">toggle2</a>
    <div class="panelThatSlides2" style="display:none;background:#eee;padding:10px;">
    <div id="content2Fades" style="opacity:0;filter:alpha(opacity=0);">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ut tortor  erat, et consectetur nisl.
</div>

HELLO WORLD

followed by the jQuery function below:-

   function toggleSlider(el) {
        var dv = $(el).next('div');
        if (dv.is(":visible")) {
           dv.animate(
                {
                    opacity: "0"
                },
                600,
                function () {
                    dv.slideUp();
                }
            );
        }
        else {
            dv.slideDown(600, function () {
                dv.animate(
                    {
                        opacity: "1"
                    },
                    600
                );
            });
        }
    }

See Demo Here

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

Prevent the left border from displaying on a link that wraps to a new line

Excuse me, I am facing an issue with a pipe separator in my subnav bar between links. The problem is that the first link on a new line retains the left-border that should be disabled. How can I prevent this border from appearing on the first link of a new ...

Even with the available space filled, the buttons in the flex box still retain a small margin

Currently, I am constructing a footer for a mobile device that needs to feature 3 buttons closely aligned with no gaps in between. The styling for the container is as follows: .NavBar { width: 100vw; float: none; display: flex; height: 3re ...

Poor height parameter

I am encountering an issue with the height of the div.divB element in this code snippet: <div class="divA"> <img src="http://lorempixel.com/1000/130"/> <div class="divB"> <span>Text text</span> </div> ...

Replace all occurrences of `<span>` with an empty string in a JavaScript string

Looking for a way to remove the span element from an HTML string in JavaScript. var htmlString = '<div>test</div><p>test</p><span class="removedata">X</span><span>test</span><span class="removedata"& ...

Is there a way to have the font size in an H1 adjust dynamically to fill the entire width of the H1 using just CSS?

My h1 Element <h1>Title Of Element<h1> The h1 has a width of 200px h1{width:200px;} Although the h1 is indeed 200px wide, the text inside does not fully utilize that width. I expected setting font-size to 100% h1{font-size:100%;} However, t ...

Repositioning the Kendo UI Validator

I've been attempting to adjust the placement of the Kendo UI Validator within my project, but modifying the properties in the related kendo CSS files (e.g. display: inline-block;) has not yet resolved the issue. So, how can I shift the position from b ...

Unable to display download dialog for Java Servlet File

I am facing an issue with my web application where users are not able to download an image file from the server. Upon hitting a button on the JSP page, an AJAX post request is made to execute a servlet that should respond by sending the image file. However ...

What is causing the action button in my MUI snackbar to occupy an additional line?

I'm currently learning how to use MUI and I'm struggling with a specific issue. The close button in my code is appearing on a separate line, but I want it to be on the same line as the word "again". I've tried experimenting with absolute pos ...

Prevent jQuery from submitting a form or allow it to submit

So, I have this form that looks like this: <form method="post" name="homeForm" id="homeForm" action="thank-you-page.php"> Now, let me show you the jQuery code associated with it: $("#homeForm").on(&q ...

Show information according to the chosen start and end dates on fullcalendar

I am currently using full calendar to display some content on a calendar. 1) By default, the calendar is showing data in all date fields. 2) I have added start and end date selectors at the top of the calendar. I want to only display data based on the se ...

Error: Unable to modify the div content - Uncaught TypeError: Unable to assign value to innerHTML of null

Attempting to update the contents of a div using JavaScript and innerHTML, but encountering an issue. After implementing the code to change the div's content, the functionality stopped working. The syntax has been double-checked. Tools being used in ...

Steps for choosing a row from a dynamic table when clicking the mouse

Is there a way to retrieve a row's value upon mouse click or by checking the checkbox in the provided HTML table? Below is the JavaScript code for fetching values from a table using Spry, sourced from an XML file: var ds1 = new Spry.Data.XMLDataSet( ...

A guide to implementing lazy loading using BXSlider

I have implemented an image gallery using Bxslider, but I am facing an issue with the loading time as there are more than 15 images in each slide. To address this problem, I want to load images one by one when a particular slide appears. I came across an e ...

Eliminate any HTML content using Jsoup while retaining the text lines

I am working with a String that contains e-mail content, and my goal is to eliminate all HTML coding from this String. This is the current code I have: public static String html2text(String html) { Document document = Jsoup.parse(html); document = ...

What is the correct method for creating text that adjusts to different screen sizes?

It's common for designers to provide me with responsive designs that adjust the text of an element depending on the screen size. On Desktop: Read more On Mobile: Read On Desktop: Download PDF On Mobile: Export On Desktop: Click here On Mobile ...

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 ...

Ways to ensure next/image takes up all available grid space

Is there a way to make the next/image element span from grid 1 through 8? It seems to work fine with the imgtag but not with next/image. .project { display: grid; margin-bottom: 4rem; grid-template-columns: repeat(12, 1fr); align-items: center; } ...

Angular components are not receiving the Tailwind CSS attributes applied to :root classes

In my Angular 12 project, I have integrated Tailwind CSS. The structure of the package.json is as below: { "name": "some-angular-app", "version": "0.0.0", "scripts": { "ng": "ng&quo ...

Ways to adjust the dimensions of an SVG icon in a React application

Trying to figure out how to display an SVG icon and text on the same line in React and Next.js. After some research, I've managed to achieve it. However, I'm struggling to control the size of the SVG icon in relation to the text. Ideally, I want ...

Exploring ways to incorporate mouse movements into this simple JavaScript script

I have recently decided to convert my JavaScript code into jQuery code to incorporate mouse events. After downloading the latest version of jQuery and renaming it as "jquery.js," I made modifications to my manifest.json file by adding "jquery.js." However, ...