I have implemented a basic JavaScript function that dynamically changes HTML content based on the width of the browser window. When in mobile view, it removes the attribute data-hover
, and when in desktop view, it adds the attribute back.
The functionality works as intended, but I am encountering an issue with Bootstrap not recognizing that the data-hover
attribute has been removed. This causes the dropdown to still close when the user's mouse leaves the menu. Essentially, I want the dropdown to remain open even if the user's mouse exits the menu area when the browser width is below 768px.
How can I rectify this situation?
JavaScript:
$(document).ready(function () {
$(window).resize(function() {
if ($(window).width() < 768) {
$('.dropdown-toggle').removeAttr('data-hover');
} else {
$('.dropdown-toggle').attr('data-hover', 'dropdown');
}
});
});
HTML:
<a class="dropdown-toggle" data-hover="dropdown" data-toggle="dropdown" role="menu" aria-expanded="false" href="/courses">
Courses
</a>
<ul class="dropdown-menu courses-dropdown">
<li>hello</li>
</ul>