Skip to main content
Asked
Viewed 1k times
0

I am showing a notification bar on some pages on my site. If the bar is showing I would like to highlight the navigation bar and move it down the page.

The css for the bar is <div id="infobar_1_1" class="info_bar" style="background-image:... <div class="infobar_inner"><span id="infobar_txt" ....</div></div>

I'm trying to use jquery code if ($('#infobar_1_1').hasClass('.info_bar')) { $(".navbar").css({"background-color":"yellow", "margin-top":"40px"});} but nothing is happening.

`$(".navbar").css({"background-color":"yellow", "margin-top":"40px"});` 

works on every page so maybe there is something wrong with the if() statement.

My site is a wordpress site. Thanks

4 Answers 4

2

Try,

if ($('#infobar_1_1').hasClass('info_bar')) { 

you dont need to specify the dot whenever using .hasClass() function

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

Comments

2

You don't need the . for the class name here when using .hasClass():

if ($('#infobar_1_1').hasClass('info_bar'))

4 Comments

I removed the . and it still isn't working. should I be using bind() instead?
Did you receive any error in the console? Seem like the error is coming from the other part of your code.
there is an console error: Uncaught SyntaxError: Unexpected end of input but this error is showing without my code and when I just add $(".navbar").css({"background-color":"yellow", "margin-top":"40px"}); the error is showing still but the code works so it doesn't seem to be the issue although I'm not sure...
also the path to the error file is not the path I'm editing so I don't think it is connected at all. (although it is also a jquery file)
2

The hasClass() method expects the class name as the parameter, not a class selector so remove the . in .info_bar

$('#infobar_1_1').hasClass('info_bar')

Comments

0
if ($(".info_bar")){ $(".navbar").css({"margin-top":"35px"});}

worked. I'm not sure what was wrong with my original code but it is working now with this code.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

').hasclass() - Stack Overflow">
Skip to main content
Asked
Viewed 1k times
0

I am showing a notification bar on some pages on my site. If the bar is showing I would like to highlight the navigation bar and move it down the page.

The css for the bar is <div id="infobar_1_1" class="info_bar" style="background-image:... <div class="infobar_inner"><span id="infobar_txt" ....</div></div>

I'm trying to use jquery code if ($('#infobar_1_1').hasClass('.info_bar')) { $(".navbar").css({"background-color":"yellow", "margin-top":"40px"});} but nothing is happening.

`$(".navbar").css({"background-color":"yellow", "margin-top":"40px"});` 

works on every page so maybe there is something wrong with the if() statement.

My site is a wordpress site. Thanks

4 Answers 4

2

Try,

if ($('#infobar_1_1').hasClass('info_bar')) { 

you dont need to specify the dot whenever using .hasClass() function

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

Comments

2

You don't need the . for the class name here when using .hasClass():

if ($('#infobar_1_1').hasClass('info_bar'))

4 Comments

I removed the . and it still isn't working. should I be using bind() instead?
Did you receive any error in the console? Seem like the error is coming from the other part of your code.
there is an console error: Uncaught SyntaxError: Unexpected end of input but this error is showing without my code and when I just add $(".navbar").css({"background-color":"yellow", "margin-top":"40px"}); the error is showing still but the code works so it doesn't seem to be the issue although I'm not sure...
also the path to the error file is not the path I'm editing so I don't think it is connected at all. (although it is also a jquery file)
2

The hasClass() method expects the class name as the parameter, not a class selector so remove the . in .info_bar

$('#infobar_1_1').hasClass('info_bar')

Comments

0
if ($(".info_bar")){ $(".navbar").css({"margin-top":"35px"});}

worked. I'm not sure what was wrong with my original code but it is working now with this code.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.