A guide to implementing toggle functionality on child elements

After clicking the button, the entire sidebar toggles, but I only want the side menus to toggle. Here is the relevant HTML code:

<nav id="sidebar">
    <div class="sidebar-header">
         <img src="/fintantra/images/logo.jpg" style="margin-left:4px;">
               <div class="btn-group">
                .....
                ......
    <ul class="list-unstyled components" id="demo">
           <li>
              <a href="#adminSubmenu" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle">
                  <i class="fas fa-user"></i>&nbsp;
                     FDR
                </a>
             <ul class="collapse list-unstyled" id="adminSubmenu">
                  <li>
                      <a href="fpo-profile.html"><i class="fa fa-arrows-alt" aria-hidden="true"></i>&nbsp;FPO Profile</a>
                   </li>
                        ....
              </ul>
             </li>
              <li>
                  <a href="#fisSubmenu" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle">
                        <i class="fas fa-user"></i>&nbsp;
                        FIS
                  </a>
                    <ul class="collapse list-unstyled" id="fisSubmenu">
                  <li>
                      <a href="fpo-profile.html"><i class="fa fa-arrows-alt" aria-hidden="true"></i>&nbsp;FPO Profile</a>
                   </li>
                        ....
              </ul>
              </li>
              ...
        </ul>
</nav>
<button type="button" id="sidebarCollapse" class="btn btn-info">
            <i class="fas fa-align-left"></i>&nbsp;&nbsp;& &nbsp;
            <span>KANCHI</span>
            </button>

Regarding the JS part:

<script type="text/javascript">
        $(document).ready(function () {
            $('#sidebarCollapse').on('click', function () {
                $('#sidebar').toggleClass('active');
            });
        });
    </script>

The current code toggles the entire sidebar. When I tried replacing ('#sidebar') with ('#demo'), it did not toggle as expected. I used 'demo' as the ID in the menu ul section.

Answer №1

<script type="text/javascript">
        $(document).ready(function () {
            $('#sidebarCollapse').on('click', function () {
                $('#demo').toggleClass('active');
            });
        });
</script>

Answer №2

<script type="text/javascript">
        $(document).ready(function () {
            $('#sidebarCollapse').on('click', function () {
                $('#demo').toggle();
            });
        });
</script>

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

Using a parameter as a key index in JavaScript

Here's the structure of my Object festivals: Object {friday: Object} friday: Object amazon: Object band: Object Next, I've created a function called`newAct`: function newAct(band, date, startTime, endTime, stage){ var ...

selenium webdriver is having trouble advancing beyond the loading stage of a javascript table

I am in the process of creating a web scraping tool to extract public data from a specific website's table Below is the code I have written for this purpose: options = webdriver.ChromeOptions() options.add_argument('--headless') driver = we ...

What is the best way to deactivate ng-repeat using a button in AngularJS?

How can I disable the apply button once it has been clicked by the user in a list of stores based on services? I am using ng-repeat to display the listing. Additionally, how can I write an apply function to disable a particular service once it has been app ...

Difficulty encountered in positioning a div element

I have been attempting to align the div with class bioDiv under the image, but despite various attempts, I find myself feeling more and more confused. Can someone please review the code for me and offer a clue? I want to retain the same appearance, just re ...

Establishing a jQuery extension

Reviewing some code on our website led me to this particular snippet: <script> (function (a) { _q = function () { return a; }; $ = function (f) { typeof f === 'function' && a.push(arguments); return $; }; ...

AngularJS Firebreath Component

Trying to integrate a FireBreath plugin object into an AngularJS view is causing an error when attempting to render the view: TypeError: Cannot read property 'nodeName' of undefined The object can be successfully included in the view using $com ...

Creating a custom design for ng-bootstrap accordion using CSS styling

I've encountered an issue with my Angular 2 component that utilizes an accordion from ng-bootstrap. While the functionality works perfectly, I'm facing a problem with applying custom styles using the .card, .card-header, and .card-block classes o ...

How to handle users with disabled Javascript? Considering redirection to alternate sites for users with script disabled?

I've dedicated today to strategizing the best approach for revamping our company's website, taking into consideration that less than 1% of web users have JavaScript disabled. Our management team strongly believes in ensuring that our website rem ...

What is the best way to use PHP to call an ACF variable and substitute a nested HTML element?

Utilizing the repeater field in Wordpress' advanced custom fields, I have made the content on my site dynamic. View an image of the static markup here The following is how I structured the content using plain HTML: <div class="container" ...

What is the process for reversing the texture application direction on a CylinderGeometry object?

Is it possible to reverse the orientation of texture mapping on a CylinderGeometry object? var obj = new THREE.Mesh( new THREE.CylinderGeometry(20, 15, 1, 20), new THREE.MeshLambertMaterial({color: 0x000000}) //the material is later changed to the ...

Tips for utilizing append and remove to modify HTML forms

Currently, I am working on a WordPress project that requires the use of append and remove for dynamically changing forms in HTML. I attempted to implement this functionality, but encountered some errors. The code snippet below provides a brief overview of ...

approach for sending an array to model-view-controller through ajax in asp.net

How can I pass an array in MVC to store data in SQL Server? I am trying to save the score of each question using Ajax and jQuery, but it only saves one piece of data from the list array. The array variable is result that needs to be assigned to eachscore. ...

Excessive Width Issue Caused by Bootstrap 4 Navbar Items

Having trouble with my Bootstrap 4 Navbar menu implementation. When I add more items, they overflow the container instead of floating correctly. Any CSS suggestions to temporarily fix this issue? <!DOCTYPE html> <html lang="en"> <head> ...

Using HTML div tags to create a looping expression that can interact with Javascript

Currently, I am working on a Ruby on Rails project and I am facing a challenge with looping variables in an HTML form to reduce the amount of manual code I have to write. However, when I attempt to evaluate an expression in the id attribute, the JavaScript ...

Creating a Node API that can patiently listen for external data

My current project involves building a server that fetches data from an external API and returns it to the endpoint localhost:3000/v1/api/. However, I'm facing a challenge where the data retrieval process takes approximately 2 seconds, leading to empt ...

utilizing two input elements within a single form each serving a unique purpose

Hello everyone, I'm seeking assistance on how to code a form with two input element tags, each having different functions. Alas, the solutions provided in this source are not working for me. Here is the troublesome source PHP CODE: <?php $ser ...

What is the best way to implement autoplay sound on a JavaScript webpage?

I'm currently working on a web system aimed at monitoring values from a database, and I have the requirement to trigger a sound alert when a specific range of values is received. Despite trying various examples found online, none of them have been suc ...

What could potentially be the reason behind the incapability of the next.js Image component to transform the svg into a

Unique Context I recently developed a minimalist Hero + Navbar using Next.js. The site utilizes the powerful next.js Image component to display images. Surprisingly, all three images on the website, which are in .webp format, load instantly with a size of ...

The necessity of ExpressJS

After reviewing the Express.JS 4.x API documentation, I became intrigued by their setup process. Here's my interpretation of how it works: In the provided sample code snippet from the Express.JS 4.x API, the express module is first imported and stored ...

Sorting information by class titles in AngularJS

Working with angularjs, take a look at my view code : <div style="width:70px;"> Show Online <input type="checkbox" ng-model="showonline" /> </div> <div ng-repeat="user in use ...