Reputation: 116
I have a question with .hasClass
method in jquery.
For example I want my menu button color to become red when my div
element with .show
class exists.
So I have written this code for explain this solution
if ($(".navbar-collapse").hasClass("show"))
$(".menuIcon").css("color", "red");
but this code didn't work when I tested... how can I fix this problem?
Upvotes: 0
Views: 55
Reputation: 77063
Your code looks to be ok, but the behavior you are experiencing can be due to one or more of the following:
script
tag if so)show
class
is created or before the show
class
is being added to the tagshow
class
might be added to another tagbackground-color
instead of color
To get a more exact response, you need to ask a more exact question.
Upvotes: 0
Reputation: 3796
your code is totally correct , and it should works . just make sure of
1- your jquery library is exists and loaded coreectly .
2- the styles associated with your code is exists in the page and true
3- contain your code inside $(document).ready(function(){ });
to ensure the styles are loaded before the code excuted .
$(document).ready(function(){
if ($(".navbar-collapse").hasClass("show")){
$(".menuIcon").css("color", "red");
}
});
Upvotes: 1
Reputation: 417
Try this:
$(".navbar-collapse.show").find(".menuIcon").css("color", "red");
You can associate it with the event that changes the menu.
Upvotes: 0