Closing the Main Dropdown Navbar in Bootstrap 4: What happens once you click on a Menu Option?

How can I close the dropdown menu in the navbar when an option is clicked after it switches to a mobile view?

Below is the HTML code for my navbar.

<nav class="navbar fixed-top navbar-expand-lg navbar-custom">
  <div class="container-fluid">
    <div class="navbar-header">
      <%= link_to locale_root_path(anchor: "top"), class: "navbar-brand page-scroll" do %>
        <span class="text-red">Light</span><span class="text-blue">Be Corp</span>
      <% end %>
    </div>
    <button type="button" class="navbar-toggler navbar-toggler-right" data-toggle="collapse" data-target=".navbar-main-collapse"> <i class="fas fa-bars"></i> </button>

    <!-- Navbar content goes here -->
    
  </div>
  <!-- /.container --> 
</nav>

I have managed to achieve this with a regular dropdown menu. For more information, visit this post.

$("#navbarDropdown").dropdown('hide')

I need assistance with implementing this feature on the navbar itself. I have tried the following approaches but none of them seem to work.

$(".collapse").dropdown('hide')

$(".navbar-main-collapse").dropdown('hide')

$(".navbar-nav").dropdown('hide')

Answer №1

After some experimentation, I was able to locate a solution that worked perfectly. It was just a matter of identifying the correct class to manipulate in Bootstrap 4.

$('.navbar-nav>li>a').on('click', function(){
    $('.navbar-collapse').collapse('hide');
});

Learn how to hide collapsible Bootstrap 4 navbar with a click

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

Tips for ensuring a div stays centered while resizing the window

When I resize the window, the div tab on the right side of the screen moves to the bottom right. How can I make it stay in the middle regardless of screen size? I've tried using margin-left:auto and margin-right:auto but it didn't work. Changing ...

Removing the rows that do not contain a table - nested div elements within other div elements

I am trying to create stripped rows with "complex" children, but I'm having trouble figuring out how to do it. I am using bootstrap, although I'm not sure if that makes any difference! This is what I have attempted so far: https://jsfiddle.net/ ...

Utilizing AJAX to amplify the worth of plupload's echo capabilities

Greetings! Here is the JavaScript code I am using for plupload var uploader = new plupload.Uploader({ runtimes : 'html5,flash,silverlight,html4', browse_button : 'pickfiles', // you can pass in id... container: document.getElementById( ...

Experiencing difficulty in adding a sub-document to an array within a parent

I am currently working with a users model that incorporates a locationsSchema: const locationSchema = require('./location.js'); const userSchema = new mongoose.Schema({ email: { type: String, unique: true, required: true, }, t ...

Leveraging JavaScript functions for invoking Ajax requests, accompanied by ASP.NET controls

Having a background in PHP, I am accustomed to using a PHP file to handle all of my ajax calls. Recently, I have been introduced to ASP.NET controls and the overall environment. I am curious about the correct method for handling ajax requests when they n ...

Looking to eliminate the bullet point next to the labels on a highcharts graph?

I have implemented a Highcharts solid gauge in my project, and you can view a sample image https://i.stack.imgur.com/QQQFn.png However, I am facing an issue where unwanted grey bullets are appearing in the test environment but not locally. I have checked ...

Issues with $mdDialog controller functionality in AngularJS Material are only occurring on certain computers

Below is the code snippet for mdDialog: function selectAddress(addressType) { $mdDialog.show({ controller: function ($scope, $mdDialog) { var savm = $scope; savm.addressType = addressType; savm.close = $mdDialog.hide; savm.f ...

Exploring the role of 'this' within a given setting

Despite my efforts, I am struggling to comprehend why 'this' is not functioning in the first code sample but works in the second one. First (doesn't work here) $('#' + ID).parent().siblings().each(function() { selectChildren( ...

Discover a method to retrieve all recommended strings based on a search query using JavaScript

I have two strings: one containing all the values of countries and the second string that I entered when searching for a country. Now, I am looking to retrieve all results that contain "In", such as India and Indonesia. For example, if I search for "IN" ...

Tips on using JQuery to extract form field information from a drop-down menu, display it in a div, and then compare it with the subsequently

In my HTML file, I am using two dropdown lists and JQuery for validation. First, I need to save the selected items from both dropdown lists in a variable and then compare them with the next selection. If the data from both dropdown lists match, an alert m ...

Tips on how to center an image within a table cell:1. Insert the image

I've been attempting to center an image within a bootstrap4 table cell, but it keeps appearing on the left and even overlaps the border. I've tried adding inline styles like text-align: center; vertical-align: middle; both to the table cell HTML ...

Tips for incorporating local storage into Angular applications

After successfully creating a table using Angular, I decided to incorporate a local storage feature. Despite my efforts, I'm struggling with implementing gsklee/ngStorage and gregory/angular-local-storage libraries into my existing code. Could someon ...

Is there a way to print the entire Bootstrap modal even if the content in the modal body is scrolled beyond visibility?

An issue arises when a user needs to scroll down to see all of the content in the modal body. When printing the modal, only the visible part is printed rather than the entire content. Various code snippets from different sources have been tried with no suc ...

Retrieving information from the second .getJSON request

I am encountering an issue where I have set up the site to make two consecutive API calls, but I am facing difficulty in fetching data from the second call and displaying it in my div. $(document).ready(function() { $(".discogs-loading").show ...

JavaScript: Implementing a seamless spinning effect for an image [Compass needle]

I have been working on developing a compass app for mobile devices and have successfully created a function that returns the change-values for the compass-needle rotation (e.g. 20). However, I have noticed that setting the 'image-transform' to &a ...

The callback assigned to the state variable of the Context Provider will not be defined

I have examined previous questions asked by OPs here, but none of the solutions seem to work for me. The issue I am facing involves a layout with a toolbar used to submit forms. To achieve this, I am employing a FormProvider as a Context.Provider that wrap ...

Unable to upload files using Jquery.form (3.51.0-2014.06.20) in IE9 within a Sharepoint application

Before diving into this issue, I want to clarify that I have thoroughly researched similar problems on Stack Overflow and other forums related to jQuery form upload problems. However, I am confident that the issue I am experiencing is unique. The problem ...

Template for Gatsby's alternative layout design

I have recently developed a template for pages that showcase a single product (blog-post.js). However, I now require a different type of page with its own template (category-post.js) to display all products within a specific category. Since the number of c ...

What is preventing the bundling of my CSS into the application?

I'm facing an issue while setting up a new project using vue.js, scss, and webpack (with express.js on the server side and TypeScript). I copied over the configurations from a previous project where everything was working fine. According to my underst ...

"Exploring the power of asynchronous operations in NodeJS using Q

Currently, I am utilizing Node.js and I aim to incorporate promises to ensure a complete response following a for loop. exports.getAlerts = function(req,res,next){ var detected_beacons = []; if (!req.body || Object.keys(req.body).length == 0) { res.s ...