Selection of two options is maintained in the dropdown menu with data toggle

Having an issue with my dropdown menu that has a data toggle for tabs. Once I select an item, it remains blue and stays selected. Any solutions?

https://i.sstatic.net/BZqoL.png

Snippet of the code causing the problem:

    <ul class='nav nav-tabs'>
    <li class='nav-item'>
      <a class='nav-link active' data-toggle='tab' href='#overall'>Overall</a>
    </li>
    <li class='nav-item'>
        <div class="dropdown">
            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              Classes
            </button>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
              <button class="dropdown-item" data-toggle='tab' href="#zombie" type="button" aria-selected="true" >Zombie</button>
              <button class="dropdown-item" data-toggle='tab' href="#spider" type="button"aria-selected="false">Spider</button>
              <button class="dropdown-item" data-toggle='tab' href="#enderman" type="button"aria-selected="false">Enderman</button>
            </div>
          </div>
    </li>
  </ul>
    <div class='tab-content'>
    <div class='tab-pane container active' id='overall'>

More content here...

Answer №1

  1. Implement a id="dropdown-menu" alongside the existing class="dropdown-menu". This setup will offer more convenience. Refer to line -20.
  2. Create a JavaScript function that will apply the .active class to the selected area/HTML element upon clicking it. For instance, when you click on

<button class="dropdown-item" data-toggle='tab' href="#zombie" type="button" aria-selected="true" >Zombie</button>

the script will add the .active class to this button,

<button class="dropdown-item class" data-toggle='tab' href="#zombie" type="button" aria-selected="true" >Zombie</button>

and remove the .active class from previously active elements. The .active class is a predefined Bootstrap class that applies CSS styles from Bootstrap framework.

See code snippet below for reference-

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
  <script type="text/javascript" src="http://getbootstrap.com/dist/js/bootstrap.js"></script>
  <link type="text/css" rel="stylesheet" href="http://getbootstrap.com/dist/css/bootstrap.css"/>
  <title>HTML5, CSS3 and JavaScript demo</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="41232e2e35323533203101756f746f72">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
</head>
<body>
  <ul class='nav nav-tabs'>
    <li class='nav-item'>
      <a class='nav-link active' data-toggle='tab' href='#overall'>Overall</a>
    </li>
    <li class='nav-item'>
        <div class="dropdown">
            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              Classes
            </button>
            <div id="dropdown-menu" class="dropdown-menu" aria-labelledby="dropdownMenuButton">
              <button class="dropdown-item" data-toggle='tab' href="#zombie" type="button" aria-selected="true" >Zombie</button>
              <button class="dropdown-item" data-toggle='tab' href="#spider" type="button"aria-selected="false">Spider</button>
              <button class="dropdown-item" data-toggle='tab' href="#enderman" type="button"aria-selected="false">Enderman</button>
            </div>
          </div>
    </li>
  </ul>
    <div class='tab-content'>
    <div class='tab-pane container active' id='overall'>
    blabla
    </div>
    <div class='tab-pane container' id='zombie'>
        zombie
    </div>
    <div class='tab-pane container' id='spider'>
        Spider
    </div>
    <div class='tab-pane container' id='enderman'>
        Enderman
    </div></div>
<script>
  var header = document.getElementById("dropdown-menu");
  var btns = header.getElementsByClassName("dropdown-item");
  for (var i = 0; i < btns.length; i++) {
    btns[i].addEventListener("click", function() {
    var current = document.getElementsByClassName("active");
    current[0].className = current[0].className.replace(" active", "");
    this.className += " active";
    document.getElementById("overall").innerHTML = this.innerText;
    });
  }
</script>
  
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="45272a2a31363137243505716b706b76">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
</body>
</html>

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

How to execute a doPostBack function within a jQuery click event

My current situation involves a js function that triggers a click event on all 'a' elements... $(document).ready(function hideRanges() { $('a').click(function (event) { $('.ranges, #UpdatePanel').hide(); }); }); ...

Is it possible to change the src attribute after the page has loaded and execute it

A. I'm trying to figure out how to dynamically change the src attribute of an image using JavaScript after the page has loaded. <img src="http://example.com/image.png" /> to <img src="http://domain.com/different.jpg" /> B. Another questi ...

CSS has the ability to override the height and width of images

<img src="img_beach.jpg" alt="Beach" width="800" height="600"> In my HTML body tag, the line above is what I have. However, the specified height and width are being replaced by the values set for the image in the stylesheet. What could be causing t ...

Displaying JSON formatted dates using AngularJS

There is a JSON file with a "DataIscr" field structured like this: "DataIscr": "1969-6-17T00:00:00+01:00". A filter has been created for DataIscr: <label for="DataIscr">DataIscr:</label> <select style="width:200px" data-ng-options="Person.D ...

Having trouble with the linear gradient color length in HTML?

I'm attempting to design a gradient background using HTML and CSS. The gradient I want should be red for the top 10% and green for the remaining 80%, similar to this example: https://i.sstatic.net/TlnIh.jpg To achieve this, I came up with the follo ...

Box-sizing:border-box causing CSS padding problem in Firefox

Something strange is happening in Firefox, it seems like the debug console is not telling the truth or there's something I'm not seeing. Here's the CSS for the body tag: html, body { width: 100%; } body { padding: 5% 10% 0 10%; ...

What is the best way to include a dropdown menu in a navigation bar?

I am looking for guidance on adding a dropdown menu to my navbar in place of the "social media" section. Below is the code snippet I have been working with: <span class="navbar-toggler-icon"></span> </button> <div class="co ...

Ways to troubleshoot and fix the 500 Internal Server Error

Why am I receiving a "500 Internal Server Error" when making an ajax call? What could be causing this issue and how can it be fixed? ...

Integrating md-chips md-separator-keys with md-autocomplete: A step-by-step guide

My experience with using md-chips and md-autocomplete reveals an issue: when I incorporate md-separator-keys, it functions as expected. However, upon adding md-autocomplete, the md-separator-keys functionality ceases to work. This is how my code is struct ...

Exploring optimal practices for CSS components and responsive design integration

If I were to develop a reusable component like a table that will be used across various pages, the content and number of columns may differ from one page to another, but the overall design should remain consistent. .my-table { background-color: ... et ...

django problem with bootstrap modal

I'm having trouble with the Bootstrap 4 modal in my Django project. When I click the button, the fade effect covers the entire screen, but the modal doesn't appear. I'm currently using Django 1.9. Does anyone have any suggestions on what mi ...

Dynamic images blending seamlessly with a fluid background aesthetic

Hello everyone, I could really use some assistance! My client is looking for a WordPress page with full screen backgrounds. Specifically, on one of the pages, they want 3 images placed on a fullscreen background image that remain in a fixed position while ...

How can I align navigation items to the right in Bootstrap 4?

Is there a way to align the navigation items to the right in a Bootstrap 4 navbar? I attempted using float-*-right but it didn't produce the desired result. <nav class="navbar navbar-static-top navbar-dark bg-inverse float-*-right"> <a c ...

Issues with a top navigation bar

I'm currently working on customizing a header menu to my liking but have limited experience with CSS. The header menu consists of two main items, each with a dropdown. I envision the first menu item to look like this upon hover: https://i.sstatic.net ...

There is an error in the regular expression that has not been caught: the forward slash is

I have a TableCell with the following structure: <asp:TableCell CssClass="plusTd button" ID="plusCell"> </asp:TableCell> Then, I decided to add a property to it like this: plusCell.Attributes.Add("onclick", "/Add.aspx ...

Spin symbol when hovering over a hyperlink

I included a link and an icon afterwards. https://i.sstatic.net/MbrSH.png <a class="link--primary" href="#">LINK<svg class="icon icon-arrow-right"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-arrow-right"></use ...

The tag that is mandatory is not functioning properly in the Bootstrap form

I am currently working on integrating a Bootstrap form into my Flask website, and I am facing an issue with making certain fields mandatory before submission. I have tried using the "required" attribute in the input tags, but for some reason, it is not wor ...

How can you make a dynamic 360 image that responds to mouse movements using three.js?

Is it possible to achieve a three.js effect similar to the one shown here? We have come across solutions that involve drag&drop for camera angle control, but we are interested in having the mouse position dictate where the camera points. For instance, ...

Revise the cascading style sheets for HTML content loaded via AJAX

I have a situation where I am dynamically loading HTML content using AJAX with JQuery. After the content is loaded, I am triggering a click event on specific elements within the newly added HTML. However, I am encountering an issue when trying to set the ...

Arrange two text elements vertically

I'm having trouble aligning these two text elements (h1 & p) vertically. It seems like the header is preventing the paragraph from moving to the left side. https://i.sstatic.net/hmdip.png .container .links h1 { font-size: 20px; float: right; ...