-2

I got this error in console log but can't determine what's wrong. I would like to hide an element when the button is collapsed

$(".navbar-toggle").click(function() {
if(".navbar-toggle").hasClass("collapsed") 
    $(".country-flags-container").hide();
else   
    $(".country-flags-container").show();
)}

I appreciate your help for my learnings.

Kobe Bryan
  • 317
  • 1
  • 3
  • 16

4 Answers4

3

Try this one :

   $(".navbar-toggle").click(function() {
    if((".navbar-toggle").hasClass("collapsed")) // missing () for if statement
        $(".country-flags-container").hide();
    else   
        $(".country-flags-container").show();
    });// this was the issue

Good luck

kourouma_coder
  • 1,078
  • 2
  • 13
  • 24
1

You are using this code :

if(".navbar-toggle").hasClass("collapsed") 

and

".navbar-toggle"

is not a selector, also if you use this :

$(".navbar-toggle")

it return an array of elements having class 'navbar-toggle'

try this :

$(".navbar-toggle").click(function() {
if( $(this).hasClass("collapsed") )
    $(".country-flags-container").hide();
else   
    $(".country-flags-container").show();
)}

also you have to think about removing the "collapsed" class after checking it, it's a flag ;)

kanzari
  • 87
  • 4
1

Because you are missing brackets for if condition. You also missed $ from (".navbar-toggle"). This should be $(".navbar-toggle"). Please try the following:

$(".navbar-toggle").click(function() {
   if($(".navbar-toggle").hasClass("collapsed")){ 
      $(".country-flags-container").hide();
   }
   else{   
      $(".country-flags-container").show();
   }
});

You can replace $(".navbar-toggle").hasClass("collapsed") by the following as well:

$(this).hasClass("collapsed")
Muhammad Qasim
  • 1,622
  • 14
  • 26
1
 $(".navbar-toggle").click(function() {
   if(".navbar-toggle").hasClass("collapsed")) 
    $(".country-flags-container").hide();
  else   
    $(".country-flags-container").show();
  )}

Try this You miss the ) for if condition

Abi
  • 724
  • 6
  • 22