Maintain Bootstrap navbar collapse open when clicked on

After trying to close the navbar by clicking outside of it, I implemented this code:

$(document).on('click',function(){              
        if(!$('#top_right_menu_btn').hasClass('open')) {
            $('#myNavbar').collapse('hide');
        }

    });  

However, I noticed that it closes the drop-down menu even when I click on it. I want to prevent it from closing when clicking on it.

Visit my site (the nav bar will appear when the screen size is below 768px)

Answer №1

After reviewing the previous answer, I have made some updates below.

Remember to execute the .collapse() function on the navbar itself and not on the toggle button.

$(".navbar-collapse").collapse('toggle');

Additionally, ensure that the click event does not occur within the .navbar element.

!$(event.target).closest('.navbar').length

Putting it all together:

$(document).ready(function () {
    $(document).click(function (event) {
        var clickover = $(event.target);
        var _opened = $(".navbar-collapse").hasClass("in");
        if (!$(event.target).closest('.navbar').length && _opened === true && !clickover.hasClass("navbar-toggle")) {
            $(".navbar-collapse").collapse('toggle');
        }
    });
});

Working plnkr 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

Guide to perfectly aligning a Bootstrap 4 toolbar

I've been trying to horizontally center a toolbar without success. Despite trying various solutions, nothing seems to work in bootstrap 4. The closest solution I found requires setting a width which doesn't really center the toolbar. Since the n ...

Autocomplete Data Origin

I'm currently exploring the use of Typeahead and implementing AJAX to fetch my data source: $(document).ready(function() { $('input.typeahead').typeahead( { hint: true, highlight: true, m ...

The div element should only be hidden on mobile devices

I've been attempting to hide a div on mobile devices using a media query, but unfortunately, it's not working as expected. When I resize the window smaller than (min-width: 1224px), the content disappears instead of being hidden. Here is a snipp ...

Encountered an issue while configuring mapping upload for Bugsnag within a React Native project

I'm in the process of incorporating Bugsnag into my React Native project. I want to make sure that any stack traces point to the correct section of the code. However, due to the need for a release app to obtain a stack trace, the source mappings are m ...

Unable to resolve Uncaught TypeError: Null property reading not possible

Although I am hesitant to seek help here, solving this seemingly simple error has proved to be quite challenging. No matter what solution I attempt, the error persists. I am currently developing an application on Google Sheets that allows users to track t ...

The process of dynamically inserting input types into a <td> element using the innerHTML tag is not functioning properly in Internet Explorer

I am facing an issue with dynamically placing input types within a <td> tag - the innerHTML() method is not functioning properly in Internet Explorer, although it works fine in Mozilla. This is how I am inserting the input types using JavaScript, wi ...

Saving a JSON file in an AngularJS variable for seamless integration with 'ng-repeat' functionality

This project aims to showcase Oracle PL/SQL records on a website. I followed the steps outlined in this tutorial () to establish a connection with the database. While I successfully stored and displayed values for a single record, I encountered difficultie ...

Is it essential to emulate mouse clicks for web scraping when the href attribute is absent?

Looking to embark on an exciting web scraping endeavor, I am aiming to gather NHL data from ttps://www.nhl.com/stats/teams. There is a convenient Excel Export button that can be located using selenium and bs4. However, hitting a roadblock at this point: ...

"Discover the magic of jQuery: Unveiling the hidden div with one simple CSS visibility change

I want to implement a functionality on my screen where the Previous button is hidden initially and only becomes visible when the user clicks the Next button. I have set the CSS property for the Previous button to hide it by default, but despite using an if ...

Exploring the use of data attributes in jQuery to access JSON objects

I have set a data-attribute for multiple elements and I am looking to access the JSON object using this data attribute in jQuery. <div class="content"> <div class="plans" data-plan="state-1"><span class="pricing-symbol">$</span> ...

Asynchronously parsing CSV files in NodeJs using the `await` keyword within the `on('data')`

I have a specific code snippet that is designed to read lines from a .csv file one by one and then store each processed row into a database const csv = require('csv-parse') const errors = [] csv.parse(content, {}) .on('data', async ...

Vuetify vueJS dialog with elevated design

Is there a way to completely remove the elevation of a v-dialog so that it has no shadow at all? The elevation property in the v-dialog itself and using the class as Class = "elevation-0" have not worked for me. Here is the link to the component ...

What's the best way to update the menu element colors as I scroll down the page?

I am looking to update the color scheme of menu elements as the page scrolls using the provided template. I tried a few methods, but they lack dynamism. How can I make it more dynamic? $(document).ready(function(){ //scroll animation $('.navigati ...

Make sure to declare rest parameters first when using Typescript

I am dealing with a function that takes in multiple string arguments and one final argument of a complex type, which is called Expression. This particular code looks like this in JavaScript: function layerProp(...args) { const fields = args.slice(0, -1) ...

HTML forms are larger in size on web pages than in Internet Explorer

https://i.stack.imgur.com/bUPtm.png I have posted the issue, and it is predominantly visual in nature. When viewed on a web browser within a C++ interface, the HTML appears significantly distorted. ...

The functionality of HTML5 Drag and Drop upload is currently disabled on versions of Internet Explorer 10 and below

I recently tried using the HTML5 DND file uploading feature from , but unfortunately, it does not seem to work on Internet Explorer versions below IE 10. Here are the error details encountered on IE browsers: User Agent: Mozilla/4.0 (compatible; MSIE 8.0 ...

Creating an opacity effect with CSS for circular images

My current challenge involves cropping an image into a circle, applying a gray filter, and overlaying text on top. The issue I'm facing is that the size of the circular clip-path changes based on the original image's dimensions due to user-upload ...

Simple HTML generator using Node.js

My preference is to write my HTML in a basic and straightforward manner, without any fancy tools. The only feature I would like to have is the ability to use an include statement to easily add header, navigation, and footer sections to each page. I'v ...

Tips for executing jsfiddle code within Joomla 3.2

I'm having trouble executing this code on my Joomla website. I have Joomla 3.2 installed with the JCK Editor, but the HTML tags are not functioning correctly. Can someone please assist me in running the following code: $("#text10").keyup(functio ...

Tips for incorporating Vue.js tag attributes in IDEA

I am using IDEA 2016.2.5 and I'm trying to incorporate Vue.js into my project. However, Vue has some tag attributes that IDEA is not recognizing and keeps highlighting as errors. I have installed the Vue Plugin and tried setting the HTML custom unkno ...