1

Been at it for hours Please help

   $("#NewsReel").click(function(){
   if( $(".block_news").css("background","url('/new_skool/images/news_1.jpg')no-repeat"))
   {
   $(".block_news").css("background","url('/new_skool/images/news(2).jpg')no-repeat");
   }
   else
   {
   if( $(".block_news").css("background","url('/new_skool/images/news(2).jpg')no-repeat"))
   {
   $(".block_news").css("background","url('/new_skool/images/news_1.jpg')no-repeat");
   }
   }
   });

Thank You

It changes the image once, but then does not fire the second if statement.

4 Answers 4

3

Getting the background value is not performed this way. You need to use:

if ($('element').css('background') == 'value') {
   ...
}

Also, there is is no guarantee that jQuery will not optimise or set the background exactly the way in which you set it. For example it might return the value as:

no-repeat url('/new_skool/images/news(2).jpg')

It's best to create two CSS classes and just use toggleClass() to change the background image for example in this demo

HTML

<ul id="NewsReel">
    <li class="block_news">news</li>
</ul>​

CSS

li {
   background:url('http://lorempixel.com/25/25/abstract/1/') no-repeat;
}

li.clicked {
    background:url('http://lorempixel.com/25/25/abstract/2/') no-repeat;
}

JavaScript (jQuery)

$("#NewsReel").click(function() {
    $('.block_news').toggleClass('clicked');
    return false;
});

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

Comments

2

Use css classes to style the backgrounds with jQuery addClass / removeClass / hasClass.

Comments

2

This line/test:

if( $(".block_news").css("background","url('/new_skool/images/news_1.jpg')no-repeat"))

...will ALWAYS return the equivalent of true (it returns a jQuery Object).

Comments

1

In all cases your setting the background. To retrieve the value of the background lose the second parameter in the css method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.