Non-jQuery UI option for creating an accordion widget

I successfully implemented a jQuery accordion feature in my project, which allows for "open accordion based on current URL" and "highlighted current articles".

Initially, everything was working fine. However, I encountered an issue when adding two additional JS files to the project.

Combining these JS files resulted in a large file size of 67KB, which is quite hefty for a JS file.

Asking for suggestions on possible alternatives or optimizations we could explore.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

<script>
$(document).ready(function() {
    $("#accordion").accordion({
    active: false,
    collapsible: true,
    autoHeight: false,
    navigation: true,
    header: '.menuitem'
     }); 
     $(".menuitem").click(function(event){
    window.location.hash=this.hash;
     });
     $(".closelink").click(function(event){
    $("#accordion").accordion({
    active: false,
     });
   });
});  
$(function(){
    var current1 = window.location.href;
    $(".nav li a[href*='"+current1+"']").parents("li").addClass("current");
});
</script>

HTML

<div id="accordion">
    <h3 class="menuitem" href="#1">FIRST SECTION</h3>
    <!-- accordion panel -->
        <div class="panelBody">
            <ul class="nav menuside">
                <li><a href="http://www.guru99.com/software-testing-introduction-importance.html">Introduction</a></li>
                    <li><a href="http://www.guru99.com/software-testing-seven-principles.html">Seven Principles</a></li>
                    <li><a href="http://www.guru99.com/software-testing-lifecycle.html">SDLC vs STLC</a></li>
                    <li><a href="http://www.guru99.com/software-testing-life-cycle.html">Testing Life Cycle</a></li>
                    <li><a href="http://www.guru99.com/manual-testing.html">Manual Testing</a></li>
            </ul>
            <!-- end accordion panel -->
        </div>
    <h3 class="menuitem" href="#2">SECOND SECTION</h3> 
    <!-- accordion panel -->
        <div class="panelBody">
            <ul class="nav menuside">
                <li><a href="http://www.guru99.com/software-testing-techniques-3.html">Use Case Testing</a></li>
                    <li><a href="http://www.guru99.com/testing-review.html">Static Testing</a></li>
                    <li><a href="http://www.guru99.com/testing-estimation.html">Estimation Techniques</a></li>
                    <li><a href="http://www.guru99.com/test-plan.html">Test Plan Document</a></li>
                    <li><a href="http://www.guru99.com/software-defect.html">What is Bug</a></li>
            </ul>

            <!-- end accordion panel -->
        </div>
    <!-- end accordion -->
</div>

Answer №1

If you're looking to create an accordion using jQuery, you may find $.slideToggle() to be quite useful. This native method allows for easy implementation of the accordion functionality. For more information on this method, you can refer to the following link: https://api.jquery.com/slidetoggle/

To get started, consider the following example:

$('.trigger').on('click', function() {
        $(this).toggleClass('open');
        $(this).next().slideToggle();
})

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

Error Compiling with Vue CLI3, jQuery, and Select2: Troubleshooting Guide

Recently, I delved into Vue.js development by creating a project using Vue CLI3. My ultimate goal is to migrate my existing app to Vue, so I decided to experiment with jQuery and Select2 as they are already utilized in my current application. Despite seemi ...

Transitioning from clip to clip-path in order to utilize percentage values

My current approach involves using a scss-based animation to create border animations with movement. The animation's core functionality relies on the use of clip. $box-width: 80px; $box-height: 50px; @keyframes clipMe { 0%, 100% { ...

D3 curved path with no circular shape

I'm trying to figure out how to create SVG arcs in HTML using d3js v3 that are not perfectly circular. The issue I'm facing is that the arcs I'm able to make end up looking like distorted circles instead of true non-circular arcs, resulting ...

Element was removed upon clicking only once

Can anyone help me figure out why the behavior of .remove() with $postNav.remove(); is different here? When you click on "I'm a tag" for the first time, both the <li> and <ol> are deleted as expected. However, on the second click, only the ...

Vue: How to access Vue instance from a function outside of its scope

In my data object, there is a small section dedicated to creating a navigation menu. This menu contains buttons that should trigger specific functions. I decided to store these functions within the data object for clarity. However, the issue arises when t ...

Safari having trouble with cross-domain cookie functionality

I currently manage 2 different websites: 3rdpartycookiemanager.com website.com When I access website: I initiate an Ajax call to: using the following jQuery call: $.ajax({ ... type: 'POST', url: 'https://www.3rdpartycookiemanag ...

To switch to desktop mode, double click; for mobile view, just tap once

I am looking to implement 2 different gestures for a specific feature in my application. Ideally, I want users to be able to double click on a card to open it in desktop view, but if they are using a phone, a single tap should suffice. How can I achieve th ...

Is it possible to have both a Navbar and Sidebar concurrently using Bootstrap?

I have a slightly unconventional request - I want to incorporate the sidebar from along with the navbar-default example from http://getbootstrap.com/components/#nav. What would be the best approach to merge these two elements? Or is there a different pa ...

Position CSS code to set FRAMESET to the middle of the webpage

I've been working on this code snippet: <!DOCTYPE html> <html> <head> <style type="text/css"> .mpanefset { top: 41px; left: 181px; position: fixed; width: 100%; height: 100% } </style> </head> <frameset cla ...

Enhanced HTML table using Bootstrap with a plethora of additional elements

I know this might sound like a silly question, but I've been struggling to create an HTML table that has the following requirements: https://i.sstatic.net/FiRSD.jpg Additionally, I want the table to be hover sensitive so that when you place your curs ...

Poor response to Ajax request when combined with an else statement

I am currently utilizing Ajax in conjunction with a form to provide user feedback upon submission, indicating whether the process was successful (no errors) or if any input is missing. My approach is quite straightforward as I am using the XMLHttpRequest o ...

Guide on centering text vertically in Bootstrap 4

As I begin my journey with Bootstrap, I encountered a challenge while attempting to vertically align text. The issue arises when displaying a modal window from a partial view and trying to showcase an icon and a title in the header: Below is the page cont ...

What is the most effective way to identify mobile browsers using javascript/jquery?

As I develop a website, I am incorporating image preloading using JavaScript. However, I want to ensure that the preload_images() function is not called for users on slow bandwidth connections. In my experience, the main demographic with slow internet spe ...

Arranging the arrow for custom sorting with w2ui.js

I am currently using w2ui and have implemented a custom sort function. While the sort function is functioning properly, I am facing an issue with the arrow not displaying in the table header. Specifically, I have applied my custom sort to the last name co ...

Responsive design for iPad and larger screens - adjusting to different screen sizes

Does anyone know of a CSS code that can effectively target both iPad and desktop devices with a max-width of 768? I attempted the following code, however it seems to only work for desktops: @media only screen and (max-width: 768px) Due to this limitatio ...

What are the best ways to utilize getElementById in React?

As a beginner in React, I am attempting to create an auto text animation for my project. While I have successfully implemented it in VanillaJS, I am facing challenges with doing the same in React. import React, { Component } from 'react' class A ...

Is there a way to integrate Prettify with Blogger/BlogSpot?

I am currently utilizing blogger.com as a platform to showcase programming texts and I am interested in incorporating Prettify (similar to Stack Overflow) to enhance the appearance of my code samples. What is the best method for installing the Prettify sc ...

Adding JSON Data to script from PHP following a JQuery Ajax Request

I'm having trouble loading my JSON Data into my script after an ajax call. The process seems to be elusive at the moment. Within a Javascript library, I have code that loads a collection of music using JSON data. Here's an example of how it look ...

How can the Flickr API be utilized with JavaScript functions?

In preparation for a project, we are required to utilize the Flickr API in order to display a collection of photos when a specific category is selected. I have successfully set up my categories on the left side of a flexbox container. However, I am struggl ...

Having trouble updating the value in the toolbar?

Here is an example that I added below: When I click the add button, the value of the "newarray" variable is updated but not reflected. I am unsure how to resolve this issue. This function is used to add new objects: export class AppComponent { ne ...