1

Can someone please help me understand why this code is not working?

$('#quick-search-header.widget-title').css('background-image', 'url(dd_includes/images/icons/sliding-menu-arrow-right.gif)');

I see through Firebug that the background-image has been removed entirely from #quick-search-header.widget-title, but the new background image above is added to element.style. Thanks.

HTML -

<div id="quick-search-header" class="widget-title">
    <p>Quick search results</p>
</div>

CSS -

#quick-search-header.widget-title{
    background: #C60B46 url(dd_includes/images/icons/sliding-menu-arrow-down.gif) right 3px no-repeat;
}

Full JS (with error code marked) -

$(document).ready(function(){

    $('input#s').val('');

    $('#quick-search-header.widget-title').live('click', function(){

        if($('#quick-search-content').hasClass('visible')){

            $('#quick-search-header.widget-title').css('background-image', 'url(dd_includes/images/icons/sliding-menu-arrow-right.gif)'); /** Not working */
            $('#quick-search-content').removeClass('visible')
            $('#quick-search-content').slideUp('600');

        } else {

            $('#quick-search-header.widget-title').css('background-image', 'url(dd_includes/images/icons/sliding-menu-arrow-down.gif)'); /** Not working */
            $('#quick-search-content').addClass('visible')
            $('#quick-search-content').slideDown('600');

        }

    });

});
6
  • 1
    you say its been added to element.style, this is correct. What do you think isn't working? Commented Jul 17, 2012 at 14:22
  • Works fine for me.jsfiddle.net/FN3uE Check if the image url is resulting in a 404 status Commented Jul 17, 2012 at 14:25
  • 2
    Just a side note: Why do you select on #quick-search-header.widget-title? As #quick-search-header is an ID it would be sufficient to only use that as your selector. Commented Jul 17, 2012 at 14:25
  • @Chandu - Thank you, it was givin a 404, which pointed me in the right direction. Even though the image was being added, it was looking at the wrong URL because, unlike the CSS, it was looking from the location of the page I was on, rather than the site root. Commented Jul 17, 2012 at 14:33
  • @JonTaylor - It wasn't showing the new image (see comment directly above this), and not being particularly familier with jQuery/DOM, it just looked a bit odd being in document.style, so I wasn't sure. Thanks. Commented Jul 17, 2012 at 14:34

3 Answers 3

4

Setting any value via the css('attributename','attributevalue') function in jQuery will add that attribute to the inline style of the element. In the inspector its often labeled element.style

If you need to do this via classes only then you can create a seperate class with the alternate background image in and switch classes by adding/removing classes from the element, this would not appear in the element.style, rather it would just switch the class and that would be displayed in the inspector instead.

You could even use the toggleClass() function which would allow you to toggle a particular or multiple classes on or off.

Docs are as follows:

toggleClass

addClass

removeClass

Or you could even do it by setting an attribute on the element using .attr('class','newClassName');

Up to you.

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

1 Comment

Thank you, the explaination is very helpful, and I did create a second class to switch to as opposed to swapping just the backgournd-image, so everything is now working.
4
  1. make sure the relative path is correct. Check 10 times the relative path against the current location: javascript:

    window.location.pathname

  2. Try: first define css as:

    .XXX { background-image: url() }

then in javaScript:

backgroundImage = "url('../images/image.png')"; //note single quotes

$(XXX).css("background-image", backgroundImage);
//or
$(XXX).css({"background-image": backgroundImage});

Comments

0

Try;

$('#quick-search-header.widget-title').css({'background-image':'url(dd_includes/images/icons/sliding-menu-arrow-right.gif)'});

3 Comments

uh well im almost certain that is wrong, the : in the css() function should be a , as it is separating parameters.
hmm interesting, ive not seen that syntax used before for this method, I will look into it.
I modified your statement as I realised what it was you were doing, you were using the map syntax but you didn't have the { } around the map. See here api.jquery.com/css/#css-map

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.