I encountered a situation where I needed to prevent the accidental closure of a panel when clicking on plain text. Unlike other solutions, my approach ensures that the panel does not close if you click on non-link text.
To achieve this, I enhanced Paul Tarr's solution by incorporating a check to determine if the click occurred within the designated area:
if ($(event.target).parents(".navbar-collapse").length < 1) { }
The revised code snippet is as follows:
$(document).click(function (event) {
if ($(event.target).parents(".navbar-collapse").length < 1) {
var clickover = $(event.target);
var $navbar = $(".navbar-collapse");
var _opened = $navbar.hasClass("in");
if (_opened === true && !clickover.hasClass("navbar-toggle")) {
$navbar.collapse('hide');
}
}
});
You can test this functionality in action through this demo fiddle, which demonstrates how clicking on non-link elements inside the panel will not result in its collapse.