I'm trying to make my code check if an element with an ID named navbarSupportedContent
has a class named show
. My code doesn't seem to work and I don't know why.
I'm using Bootstrap, so Bootstrap will do most of the work for me but whenever I press a button a class named show
will be a added to the html element below.
Whenever the class show
is detected on #navbarSupportedContent
the function should trigger but nothing happens.
$(document).ready(function() {
$("#navbarSupportedContent").change(function() {
console.log("Change detected");
if ($('#navbarSupportedContent').hasClass('show') == true) {
console.log("Scroll is locked");
$('body').addClass('lock-scroll');
} else {
console.log("Scroll is unlocked");
$('body').removeClass('lock-scroll');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="navbarSupportedContent" class="collapse navbar-collapse"></div>