Experiencing excessive sliding down of JQuery accordion repeatedly

I have a unique accordion feature that expands to display further information when each button is clicked. While it works fine, I am facing an issue where the box seems to slide up and down multiple times before showing the full content. Ideally, I would like it to slide up and down just twice or maybe three times at most before displaying the complete content. Can you suggest the best way to achieve this?

Here is the HTML code:

  <div id="ss_menu">
    <h3><b>Winning Ways</b></h3>
  <div class="ss_button">1990-1991</div>
  <div class="ss_content">1st NBA Title<br />
  61 Wins 21 Losses<br />MVP Michael Jordan<br />
  Defeated LA Lakers<br /></div>
  <div class="ss_button">1991-1992</div>
  <div class="ss_content">Repeat Champions<br />
  67 Wins 15 Losses<br />MVP Michael Jordan<br />
  <div class="ss_button">1992-1993</div>
  <div class="ss_content">ThreePeat Champions<br />57 Wins 25 Losses<br />
  MVP Michael Jordan<br />Defeated Phoenix Suns<br /></div>
  <div class="ss_button">1995-1996</div>
  <div class="ss_content">1st NBA Title in 3 years<br />
  72 Wins 10 Losses<b>(NBA history)</b><br />MVP Michael Jordan<br />   
  <b>*Michael Jordan came back from retirement!</b><br />Defeated LA 
  Lakers<br /></div> <div class="ss_button">1996-1997</div>
  <div class="ss_content">2nd Repeat<br />69 Wins 13 Losses<br />
  MVP Michael Jordan<br />Defeated Utah Jazz<br /></div>
 <div class="ss_button">1997-1998</div> <div class="ss_content">
  2nd ThreePeat<br />62 Wins 20 Losses<br />MVP Michael Jordan<br />
  Defeated Utah Jazz<br /></div>
  </div>

And the CSS code for styling:

 body {
font-family: Arial, Helvetica, sans-serif;
}

#ss_menu h3{ 
color: white; 
}

#ss_menu { 
width: 200px; 
position:absolute;
right:10px; 
top:825px;
z-index:1
}

.ss_button { 
background-color: black;
border-bottom: 1px solid #FFFFFF;
cursor: pointer;
padding: 10px;
color: #FFFFFF;
    border-radius: 5px;

}

.ss_content {
background-color: #EFEFEF;
display: none;
padding: 10px;
    border-radius: 5px;

}

Lastly, here is the JavaScript function:

 jQuery(function () {
 jQuery('.ss_button').on('click', function () {
    jQuery('.ss_content').slideUp('fast');
    jQuery(this).next('.ss_content').slideDown('fast');
});
});

Answer №1

In the text from 1991-1992, there seems to be a mistake where br is being used instead of closing with div. The recurring issue appears to revolve around Michael Jordan. Make sure to verify that your code is functioning correctly by visiting https://jsfiddle.net/UniqueCoder/jbz6dxbp/. It may look odd, but it does the job.

  <div class="ss_button">1991-1992</div>
  <div class="ss_content">Repeat Champions<br />
   67 Wins 15 Losses<br />MVP Michael Jordan
  </div>

Answer №2

Your code seems to be functioning properly. It's quite peculiar, isn't it? You might want to consider utilizing the preventDefault() function. Give that a try and see if it resolves the issue.

    jQuery('.ss_button').on('click', function (event) {
     event.preventDefault();
    jQuery('.ss_content').slideUp('fast');
    jQuery(this).next('.ss_content').slideDown('fast');
});

Answer №3

It's possible that you might be binding the event handler multiple times? This could happen due to window resizing, ajax loading, or adding elements dynamically. You can test this by adding a "processed" class to your ".ss_buttons" right after attaching the click event, and then select using the ".ss_button:not(.processed)" selector.

jQuery(function () {
 jQuery('.ss_button:not(.processed)').on('click', function () {
    jQuery('.ss_content').slideUp('fast');
    jQuery(this).next('.ss_content').slideDown('fast');
 });
 jQuery('.ss_button:not(.processed)').addClass('processed')
});

EDIT: There also seems to be an issue in the HTML code as pointed out by vixed:

<div id="ss_menu">

 <h3><b>Winning Ways</b></h3>

 <div class="ss_button">1990-1991</div>
  <div class="ss_content">
    1st NBA Title<br />
    61 Wins 21 Losses<br />MVP Michael Jordan<br />
    Defeated LA Lakers<br />
  </div>

 <div class="ss_button">1991-1992</div>
  <div class="ss_content">
    Repeat Champions<br />
    67 Wins 15 Losses<br />
    MVP Michael Jordan<br />
 ***** <-- Should'nt be here a "</div>"

 <div class="ss_button">1992-1993</div>
  <div class="ss_content">
    ThreePeat Champions<br />

 ...

</div>

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

Angular 2 component hierarchy with parent and child components

Currently, I am in the process of learning typescript and angular2. My attempt to incorporate parent and child components into my fiddle has not been successful. Despite conducting some research, I have encountered an error that states: Uncaught ReferenceE ...

Manipulating the distinct look of the final element in an *ngFor loop

I am trying to enhance the appearance of the last line of controls generated by an iterator by making it disabled and somewhat invisible. Currently, my code is functioning well, as shown below. <div *ngFor="let item of data; let last = last;"> &l ...

Choosing items by pressing "shift + up arrow"

I have a collection of elements, each representing one line of text. When you click on an element, it changes color. If you then hold down "shift + arrow up," the items above it are also selected. How can this functionality be implemented? My initial app ...

Implementing Jquery Ajax pagination with dynamic content loading

As a newcomer to ajax and jquery, I am currently working on building a search page that will contain numerous records and therefore needs to be paged. While researching tutorials on how to accomplish this task, most examples I found included page numbers d ...

jQuery Issue: Submission Count Doubling with Each POST Request

My current issue involves using jQuery to send data to a PHP file, but I've noticed that each time I submit the form, the data gets duplicated. Initially, it posts once when I press the submit button. However, upon returning to update the form and sub ...

Intersecting object identification, fresh Function

I'm currently utilizing the "Sinova / Collisions" library on GitHub along with Node.js. The library can be found at https://github.com/Sinova/Collisions. I am in need of a function that allows me to delete all data at once, as the current function for ...

Is there a way for me to make my navigation fill the entire height of the header?

I am trying to create a navigation menu within a header div where the list items are displayed horizontally inside a ul element. I want these list items to be 100% the height of the header. Despite trying to set the height to 100% on various elements such ...

Enhance your PrimeNG p-calendar by customizing the background-color of the dates

Hello, I am currently attempting to customize the appearance of the p-calendar, but I am struggling with changing the color of the displayed dates. Can anyone provide assistance? Thank you in advance. Below is my template: <div class="p-field p-co ...

Tips for implementing a document ready function on a nested page within a larger full-page website

I am currently working on a website that utilizes fullpage.js, but the same principle applies to all single-page websites. I am trying to figure out how to implement the $(document).ready() function on a 'nested' page within the site. Since every ...

Unable to pass data to Laravel 5.1 controller using Ajax Load function

Jquery Ajax Request var pageNumber = $(this).attr("data-page"); $("#results").load('pagination',{"page":pageNumber}, function(){ }); Route Configuration File Route::get('pagination',"MyController@pagination" ); Server-Side Contr ...

Utilize TypeScript File array within the image tag in HTML with Angular 2

I am in the process of developing a web application that allows users to upload CSV data and images, which are then displayed on the application. However, I have encountered an issue where I am unable to display the imported images. The images are imported ...

Putting off the execution of a setTimeout()

I'm encountering difficulties with a piece of asynchronous JavaScript code designed to fetch values from a database using ajax. The objective is to reload a page once a list has been populated. To achieve this, I attempted to embed the following code ...

Using express.js to transfer an uploaded image to Amazon S3

Currently, I am faced with an issue of passing an image uploaded from a react application through express to a managed s3 bucket. The s3 bucket is created and managed by the platform/host I am using, which also generates upload and access urls for me. So f ...

Error parsing XML: Missing opening tag

function fetchUserDetails() { $url = "http://steamcommunity.com/profiles/{$this->steamID64}/?xml=1"; $xmlData = simplexml_load_string($url); if(!empty($xmlData->error)) { return NULL; } $this->displayName = (string) $xmlData-&g ...

What causes the conflict between Nodejs usb module and Electron?

Apologies for the lengthy post, but I've been tirelessly searching for a solution online without any luck. My goal is to create a basic Electron application that can display an HTML page (which is working fine) and control a printer through the USB mo ...

Mastering the art of implementing dynamic template variables in TinyMCE

After exploring the 'full' example and conducting research on the Wiki and moxie forums, I have yet to find a solution. I am attempting to implement what the wiki states is possible, but encountered an issue when replacing the 'staffid' ...

Can you explain the significance of verbosity in a nodemon setup?

Can someone explain the verbose setting in my nodemon configuration and what impact it has on the project? { "verbose": true, "watch": "./server" } I have checked the readme file for nodemon, but it doesn't provide any information on this specif ...

Focus on the image displayed through instafeed.js

Using the Instafeed.js plugin to create a dynamic Instagram feed. Everything is working smoothly, but I'm struggling to center the images being retrieved by Instafeed. Despite trying various methods, I can't seem to get it to display as desired. ...

Is there a way to conceal the text box in jquery during the initial loading phase?

I have encountered an issue while working on a project. My code only functions properly when I click after it has loaded. I have assigned IDs to each user and implemented code for toggling active/inactive users by passing the ID to each textbox and span el ...

eliminate the gap in the MUI Accordion when it is expanded

How can I prevent the Accordion MUI component from moving and applying top and bottom margins to summary elements in expanded mode? I added the following code to the summary element but it doesn't seem to be working. Any suggestions on how to fix this ...