Reputation: 57
I need help changing my bootstrap navbar color when navbar-toggle is clicked. it just wont work.
$( ".navbar-toggle" ).click(function() {
$( ".navbar" ).css( "background","yellow" );
})
Upvotes: 2
Views: 4934
Reputation: 241
bootstrap 3
$(".navbar-toggle").click(function(event, c){
$(event.target).addClass("disabled");
$("nav").toggleClass("navbar-yellow");
setTimeout(()=>{
$(event.target).removeClass("disabled");
},500);
})
Or for bootstrap 4 and 5
$(".navbar-toggler").click(function(event, c){
$(event.target).addClass("disabled");
$("nav").toggleClass("navbar-yellow");
setTimeout(()=>{
$(event.target).removeClass("disabled");
},500);
})
CSS
.navbar-yellow {
background-color: yellow !important;
}
.disabled {
pointer-events: none;
}
http://jsfiddle.net/rdx7j5a9/34/
Upvotes: 1
Reputation: 6883
Here is the jsFiddle
$(".navbar-toggle").click(function(){
$("nav").toggleClass("navbar-yellow");
})
Create this class in css
.navbar-yellow{
background-color: yellow !important;
}
Its navbar-default that is giving the gray color.
Upvotes: 4
Reputation: 1231
You should create a custom css class and add it when user interact with your UI.
//into your css file
.navbar.activated {
background: yellow;
}
//into your js file
$( ".navbar-toggle" ).click(function() {
$( ".navbar" ).addClass( "activated" );
});
Upvotes: 1
Reputation: 8366
Change this:
$( ".navbar" ).css( "background","yellow");
to this:
$( ".navbar" ).css( "background","yellow !important");
Bootstrap default navbar css is probably overwriting the one you are adding. Use !important
to overwrite it.
Upvotes: 1