-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.

5
  • 1
    For syntax errors in JavaScript, it's simple to use JS Hint which will tell you the line numbers of the syntax errors. Commented Mar 31, 2017 at 11:35
  • You should add one more ) for if statement like if(".navbar-toggle").hasClass("collapsed") ) Commented Mar 31, 2017 at 11:36
  • There is only one syntax error in code which is in last.... i.e }) replaced by )}...so why all of giving same type answers... @ Kobe Brayan You can use a good text editor(like sublime) for avoiding syntax errors like this...() Commented Mar 31, 2017 at 11:40
  • I will try to use Sublime. I currently using notepad++ @AbhishekMishra Commented Mar 31, 2017 at 11:47
  • Sounds good :) @KobeBryan Commented Mar 31, 2017 at 11:49

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

4 Comments

it's not the only issue. spot the other and my downvote becomes an upvote.
I gave you the upvote anyway, your answer combined with the other answers will get the op to solve it. - edit - you found it ;)
Thank you so much @lazy_coder! Appreciated it a lot!
@KobeBryan can you please accept the answer so he gets the points?
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") 

2 Comments

stackoverflow.com/questions/4797286/… so only the missing ) is an error.
Thank you so much @Muhammad Qasim! Appreciated it a lot!
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

1 Comment

Thank you so much @Abi! Appreciated it a lot!
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 ;)

5 Comments

At the very least take the time to explain what the problem was, and the changes you made.
sorry, i changed : if(".navbar-toggle").hasClass("collapsed") by if( $(this).hasClass("collapsed") )
Please edit your answer to include the explanation where it's likely to be read., which also allows code to be readable and offers more space for a coherent, formatted presentation.
Thank you so much @kanzari and @DavidThomas!
@Kobe, happy coding!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.