"What is the best way to switch between multiple data targets and toggles in bootstrap using the HTML data toggle attribute

I am currently coding an animated hamburger icon for the Bootstrap navbar, and I need to find a way to toggle both the hamburger icon and the responsive navbar. Is there a method or code snippet that allows me to toggle these two elements independently using different IDs? Additionally, I am unable to utilize JavaScript because the sliding animation on the Bootstrap navbar doesn't work properly when toggled through JavaScript.

Below is the code I have implemented to target the collapsible navbar feature:

{% load staticfiles %}

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Bootstrap Example</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
        <link href="\static\base\css\hamburgers.css" rel="stylesheet">
        <link href="\static\base\css\navbar.css" rel="stylesheet">
    </head>
    <body>
        <nav class="navbar navbar-expand-md bg-dark navbar-dark">
            <button class="hamburger hamburger--squeeze" height="15" id="hamburger" data-target="#collapsibleNavbar" data-toggle="collapse">
          <span class="hamburger-box">
            <span class="hamburger-inner"></span>
          </span>
        </button>
            <a class=" navfont navbar-brand mx-auto" href="/account">Gucci Enterprises</a>
            <div class="navshow">
                {% if user.is_authenticated %}
                    <a href="/account/logout" class="navfont important">Logout</a>
                {% else %}
                    <a href="/account/login" class="navfont important" >Login</a>
                {% endif %}
            </div>
            <div class="collapse navbar-collapse" id="collapsibleNavbar">
                <div class="container">
                    <div style="text-align: center">
                        {% if user.is_authenticated %}
                        <ul class="navbar-nav">
                            <li class="nav-item">
                                <a class="nav-link navfont" href="/account/profile">Profile</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link navfont" href="/account/profile/edit">Edit Profile</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link navfont" href="/account/change-password">Change Password</a>
                            </li>
                        </ul>
                        {% else %}
                        <ul class="navbar-nav">
                            <li class="nav-item">
                                <a class="nav-link navfont" href="/account/register">Register</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link navfont" href="/account/reset-password">Reset Password</a>
                            </li>
                        </ul>
                        {% endif %}
                    </div>
                </div>
            </div>
            <div class="navhide">
                {% if user.is_authenticated %}
                    <a href="/account/logout" class="navfont important">Logout</a>
                {% else %}
                    <a href="/account/login" class="navfont important">Login</a>
                {% endif %}
            </div>
        </nav>
        <br>
        {% block body %}
        {% endblock %}

    </body>
</html>
'''

Answer №1

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>


<button class="btn btn-primary" data-toggle="collapse" data-target=".it-works">It Works</button>

<div class="collapse it-works">Yep.</div>

<div class="collapse it-works">It works.</div>

To toggle multiple elements, add the class .multi-collapse to them and use data-target=".multi-collapse" with a class instead of an id. The bootstrap collapse component multiple targets documentation suggests using a JQuery selector to reference them and provides a working example.

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

Is there a way for me to display the image name at the bottom before uploading it, and have a new div created every time I upload an image?

Is there a way to display the image name at the bottom and have it create a new div every time an image is uploaded? I would appreciate any help with this... <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstra ...

When a click event triggers, use the .get() method in jQuery to retrieve the content of a page and then update

I am currently facing an issue with a div class="contentBlock", which is acting as the container for updating content. <div class="contentBlock"></div> Unfortunately, the script I have written does not seem to be working properly :c $(docume ...

Tips for avoiding the display of the overall scrollbar while utilizing grid with an overflow:

In my react-redux application, I am utilizing the material ui Grid component for layout design. The structure of my code is as follows: <div> <Grid container direction="row" spacing={1}> <Grid item xs={3}> ...

Creating manageable CSS styles for a wide variety of themes

I need to add themes to a web application that allows users to switch between them seamlessly. The designers want a variety of font colors and background colors, around 20 in total. Is there a way to achieve this without creating multiple .css files, which ...

Struggling to find the width of an SVG text box or add line breaks after a specific number of characters?

I am currently working on creating an SVG text box using the amazing Raphael library. The content inside this text box comes from a dynamic string that is extracted from an XML document. There are times when this string turns out to be longer than the can ...

small screen right-side navigation with Bootstrap 4

My goal is to have the logo image on the left and the navigation on the right for xs devices, and the logo in the center with navigation on the left for sm devices. However, I am facing an issue where the navigation does not align to the right on xs device ...

Calculate the number of input boxes that have been filled

<input type="number" name="day1"> <input type="number" name="day2"> <input type="number" name="day3> Can anyone help me figure out how to count the number of filled fields in this form? For example, if only day1 is filled, the count resu ...

Using ng-class directive with condition in AngularJS

Here is a unique pop-up that allows users to encode item information. https://i.sstatic.net/sn9QZ.png I need to implement a feature where, if both the Foreign Currency and Conversion Rate fields have values, the Amount should be calculated by multiplying ...

Filtering HTML tables to display only one instance of each value

I have a 300x11(rowXcolumn) html table that I wanted to filter like Excel or Google Sheets's filter. Upon researching, I came across the code below on a website. While it works as desired, there is an issue where it displays the same value multiple ti ...

"Implementing a dynamic loading effect in Highcharts triggered by a button

Take a look at this sample highchart fiddle: http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/members/series-setdata/ $('#button').click(function () { var chart = $('#containe ...

Should we still be using <input type="date" /> in 2021?

I've come across some old articles cautioning against using , but they were written a couple of years ago. I'm wondering if it's safe to use in 2021 without negatively impacting the user experience. ...

Is it possible to validate only the fields that are currently visible using HTML form validation

I need a solution for excluding hidden fields from HTML form validation. My situation involves having two groups of form fields, each with some required attributes. Depending on user selection, one group may be hidden, and those fields should not contribut ...

Adjust the SVG to match the dimensions of the label, which may vary in both

How can I efficiently resize and embed SVG images into checkbox labels without using a viewbox attribute? Each SVG varies in height and width, making it challenging to find the best solution. .check__button { display: none; } .check__icon { display ...

jqGrid is throwing an error: undefined is not recognized as a function

Currently, I am in the process of trying to display a basic grid on the screen. Following the instructions provided in the jqGrid wiki, I have linked and created scripts for the required files. One essential css file, jquery-ui-1.8.18.custom.css, was missi ...

Ways to trigger a modal programmatically in the backend code?

My goal is to trigger the display of a modal when a button is clicked. The modal should be shown by clicking this button: <asp:Button ID="Button4" runat="server" class="btn btn-info" data-toggle="modal" OnClientClick="return false;" data-target="#View1 ...

The background image fails to display on the button

Why isn't the background image for my button appearing when set using a CSS rule? I have a button with the class "todo" and although other parts of the rule work, the image itself does not show up even after trying "background: url" and "background-im ...

Certain browsers have difficulty running CSS keyframes animations

As I work on my website, I have integrated a CSS keyframes animation that is triggered by clicking a link connected to a JavaScript function. Here is an excerpt from my CSS file: #banner { background-color: #00BBD2; position: absolute; width: 100%; heigh ...

Choose a selection from the options provided

This is a sample for demonstration purposes. I am trying to display an alert with the message "HI" when I click on Reports using the id main_menu_reports. My attempted solution is shown below. <ul class="nav" id='main_root_menu'> & ...

Creating dynamic div elements using jQuery

I used a foreach loop in jQuery to create some divs. Everything seems to be working fine, but for some reason, my divs are missing SOME class properties. Here is the code snippet I am using: $("#item-container").append("<div class=\"panel col-md-2 ...

Create a duplicate <li> element and animate it using jQuery

Here are the list items: <ul> <li>...</li> <li>...</li> <li>...</li> <li>...</li> <li>...</li> <li>...</li> <li>...</li> <li>...</li> <li>...</l ...