Tips on moving down the `<li><span>` elements with the jQuery `.slidedown()` method

I'm struggling to articulate my question clearly, so please bear with me.

Essentially, I have a jquery script that reveals content within a div nested in an li. When I click on the header, the content drops down and visually pushes the next header down. However, if I click anywhere on the displayed content, it doesn't slide back up but instead reveals the next content.

Test Page <~~ you can find an example of this behavior on the test page by clicking on the + header and trying to close it.

I'm at a loss as to what's causing this issue.

EDIT I've noticed that the "+" sign appears just above the picture in the "Create your own fundraising page" section, where the next collapsed header should show up. The plus sign is contained within an absolute-positioned span, so shouldn't it also move downward?

HTML:
    <ul id="toggle-view">
        <li>
            <h6>Choose a fundraising idea</h6>
            <span>+</span>
            <div class="panel">
                <p>There are countless ways to contribute! Shave your head, run a marathon, or give up your birthday - the choice is yours! We've listed some of our most popular fundraising ideas but feel free to come up with your own by choosing the 'Other' stream.</p>

                <br>

                <img src="../images/content/pagebuilder/ChooseAStream_screenshot.png" alt="Choose a Stream screenshot" width="100%"/>

            </div>
        </li>
        <li>
            <h6>Create your own fundraising page</h6>
            <span>+</span>
            <div class="panel">
                <p>Click on 'Register' to set up a fundraising page. You can set one up on your own or create a page where friends or colleagues can join you. Let us know what kind of fundraiser you're holding and we'll provide you with all the tools to help you succeed.</p>

                <br>
                <img src="../images/content/pagebuilder/PersonalPage_screenshot.png" align="left" alt="Personal page screenshot" width="100%"/>

            </div>
        </li>
        <li>
            <h6>Access your Participant Centre</h6>
            <span>+</span>
            <div class="panel">
                <p>As a Canadian Cancer Society cancer fighter, you have access to an online Participant Centre where you&#8217;ll find customizable, interactive tools to get people excited about sharing in your fundraising success!</p>

                <img src="../images/content/pagebuilder/ParticipantCentre_screenshot.png" align="left" alt="Participant Centre screenshot" width="100%"/>

            </div>
        </li>
  </ul>

CSS:
/*Drop Down Content*/
#toggle-view {
    list-style:none;    
    font-family:arial;
    font-size:11px;
    margin:0;
    padding:0;
    width: 50%;
}

    #toggle-view li {
        margin:10px;
        border-bottom:1px solid #ccc;
        position:relative;
        cursor:pointer;
    }

    #toggle-view h6 {
        margin:0;
        font-size:14px;
    }

    #toggle-view span {
        position:absolute;
        right:5px; top:0;
        color:#3498db;
        font-size:13px;
    }

    #toggle-view .panel {
        margin:5px 0;
        display:none;
    }

jQuery:

   $(document).ready(function () {

        $('#toggle-view li').click(function () {

            var text = $(this).children('div.panel');

            if (text.is(':hidden')) {
                text.slideDown('200');
                $(this).children('span').html('-');     
            } else {
                text.slideUp('200');
                $(this).children('span').html('+');     
            }

        });

    });

Any advice would be greatly appreciated. Thank you for taking the time!

Answer №1

The issue at hand is that the li element contains a floated image without any float-clearing mechanism following it. As a result, the computed height of the li does not account for the image's height, causing the + symbol to appear above the image where it aligns with the CSS.

Furthermore, this setup causes the image to overlap into the space of the subsequent li, mistakenly triggering the click event for the next content instead of the intended current content. To remedy this situation, incorporate a straightforward clearfix rule into your CSS code:

#toggle-view li::after {
    content: "";
    display: block;
    height: 0;
    visibility: hidden;
    clear: both;
}

Upon implementation, this rule will establish a virtual block after each li item within your toggle-view ul, ensuring that any floated elements inside the li are cleared appropriately.

Answer №2

$('#toggle-view li').on('click', function() {

  var content = $(this).children('div.panel');

  if (content.is(':hidden')) {
    content.slideDown('200');
    $(this).children('span').append("<span> - </span>");
  } else {
    content.slideUp('200');
    $(this).children('span').append("<span> + </span>");
  }

});

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

Steer clear of including numerous variable values in Angular 2 while adjusting the class of selected items

Here's a question from someone who is new to Angular 2 and looking for an efficient way to change the active CSS class of tabs without using the router: activeTab: string; switchActiveTab(newTab: string) { this.activeTab = newTab; } <div clas ...

Tips for embedding HTML code within {{ }} in Django templates

I'm curious about how to incorporate HTML within {{ }}: {{ article.tags.all|join:", " }} This code snippet returns tags separated by commas, for example: sports, cricket, nature, etc. What I would like to achieve is: {{ <span class="label"&g ...

Access a designated tab using cbpFWTabs

I am currently using the cbpFWTabs plugin from Tympanus for my tabs (http://tympanus.net/Development/TabStylesInspiration/). However, I am struggling to open a specific tab upon page load. I have attempted to create a show method within the page script, bu ...

Is there a way to avoid reaching the limit of 150 JSON calls per hour allowed by the Twitter API and prevent Twitter from dropping unnecessary cookies?

I currently have a Twitter feed integrated on my website using the jquery tweet plugin from tweet.seaofclouds.com along with other json plugins that fetch data from a third-party website. The issue I'm facing is that the Twitter API restricts the num ...

The JSON parser encountered an error: JSON input abruptly ended without completion

After spending two days reading and searching, I still can't seem to figure it out. Perhaps there's a small error in my code that I'm missing. The error message I'm encountering is JSON SyntaxError: Unexpected end of JSON input(…) un ...

Encountering Issues with Ajax Request in the Google Play App

I am facing an issue with my Phonegap app. It functions perfectly in debug mode and when installed as an apk file from Phonegap. However, when I upload it to Google Play Store and then download it from there, my ajax requests stop working. I have added the ...

Updating a dropdown select in jQuery using AJAX and returning an empty string when it changes

Could someone please help me figure out what I am doing wrong here? When I use console.log(), it is returning an empty string instead of an array. I am trying to update the second select box when the onChange event is triggered on the first select box, bu ...

Methods for adjusting columns and rows in HTML5 tables

As I was preparing to create a compatibility guide, I noticed that the HTML code consisted of a table. While tables are effective for displaying data, they can be cumbersome to edit frequently. What tools are available to quickly and cleanly edit an exist ...

Using jQuery to autofill a hidden input while preserving the current information

I'm trying to populate a hidden input form with the values of all checkboxes on a page while retaining their prepopulated values. I'm currently using the code below but can't figure out how to achieve this. Any help would be greatly apprecia ...

Utilizing Ajax in PHP to Upload Files

I'm encountering an issue while attempting to upload a file and send it via AJAX. The error message I am receiving is as follows: Notice: Undefined index: xlsFile in Here is the code snippet: HTML FORM : (this form is within a Modal Popup) <f ...

Is there a method available that would allow me to display my HTML code within a frame using Selenium WebDriver and Java?

Currently, I am in the process of automating an Application that includes several embedded iframes. I am wondering if there is a method to view my HTML source code within these frames. ...

Trouble looping through Javascript

Hello, I am encountering a problem with some JavaScript code that I am trying to implement. The functions in question are as follows: var changing_thumbs = new Array(); function changeThumb(index, i, thumb_count, path) { if (changing_thumbs[index]) { ...

calculation of progress bar advancement

I want to make the progress bar in my game responsive to changes in the winning score. Currently, it moves from 0% to 100%, which is equivalent to 100 points - the default winning score. But I need the progress bar to adjust accordingly based on the user-i ...

Injecting SVG styling into HTML using the content tag and CSS

I am working with 3 different files: index.html <!DOCTYPE html> <html> <head> <link type="text/css" rel="stylesheet" href="style.css" /> </head> <body> <i class="logo myRed" aria-hidden="true"></i> ...

comparing multiple values separated by commas with JavaScript

I need validation using a comma-separated value format. Within the illustration, there are two fields: "Saloon Price" (value: 10,10,10,10) and "Saloon Offer Price" (value: 11,11,11,11). The first value must be less than the second. Saloon price Value & ...

Exploring elements using dynamic xpaths in selenium

When working with Selenium WebDriver, I am facing difficulties selecting the <input class="cs-autocomplete-input" tabindex=""> element due to its fluid ID. This is because there are multiple items with the same class name on the web page, making it c ...

Show advancement bar during the process of creating an Excel spreadsheet in php

I'm currently working on an HTML form that generates an Excel file using PHPExcel when submitted. Everything is functioning correctly, but I've noticed that the process takes a long time for large excel files. I would like to implement a progress ...

Unable to assign a value to the HTMLInputElement's property: The input field can only be set to a filename or an empty string programmatically

When attempting to upload an image, I encountered the error message listed in the question title: This is my template <input type="file" formControlName="avatar" accept=".jpg, .jpeg .svg" #fileInput (change)="uploa ...

Are you interested in designing a bootstrap album for your homepage?

I'm currently working on designing an album-style layout similar to the examples provided below: if you look at the recent works section where you have an image on one side and text on the other, occupying the entire width of the page: or the example ...

An issue with the form.submit function within an if-else statement

Can anyone help me with submitting a form using AJAX within an if-else statement? I have the code below, but it doesn't seem to be working as expected: $.ajax({ type: "POST", url:'<?php echo base_url() ?>signup/orde ...