views:

494

answers:

3

I have a div in an html file defined as follows:

<div id="rangeTabAll"></div>

In an externally defined style sheet I define the following style.

#rangeTabAll{
    top:45px;
    left:124px;
    width:186px;
    height:114px;
    display:none;
    background:#ffffff url(../images/rangetab0.jpg) no-repeat;
}

How do i read/write the background value using jQuery? The

$('#rangeTabAll').css('background')

method wont work because its not an inline style. And

$('#rangeTabAll').attr('class')

is undefined.

I can do this with javascript quite simply but i wonder how its done via jQuery.

+5  A: 

background is a "magic" CSS property that is expanded to all the different background-* properties, such as background-image, background-color, background-repeat...

To get them in jQuery you would call $('#rangeTabAll').css('backgroundColor') and so on. Note the camelCase instead of separating the words with dashes.

I just noticed that jQuery converts background-color to backgroundColor etc. for you, so you won't have to worry about that. You can do $('#rangeTabAll').css('background-color') as well.

Blixt
Hi thanks, thats allowed me to read the property and technically set the property but no visual change. Please see my edit for more complete explaination.
rism
Note that the relative paths in your JavaScript are relative to the current document, while the relative paths in your CSS are relative to the stylesheet path. So if you just reused the path from your stylesheet you might be trying to get the image from the wrong folder. But as you can see, the JavaScript works. It sets the value to what you want. There's nothing that can be done to the code that makes it work better. The error is either in the image path or in your CSS (something obscuring/overriding the background.)
Blixt
Also, you should use Firefox with Firebug to inspect these values and see what went wrong (it can even show you the exact path Firefox tried to get the background image from and if it succeeded/failed.) Get Firebug here: https://addons.mozilla.org/en-US/firefox/addon/1843
Blixt
You're right. My secondary problem was a blocking typo. Thanks very much.
rism
+2  A: 

remember:

Shorthand CSS properties (e.g. margin, background, border) are not supported. For example, if you want to retrieve the rendered margin, use: $(elem).css('marginTop') and $(elem).css('marginRight'), and so on.

from jQuery Documentation: CSS

in your example, the shorthand background is not supported, you need to write the full property.

for

#rangeTabAll {
    top:45px;
    left:124px;
    width:186px;
    height:114px;
    display:none;
    background:#ffffff url(../images/rangetab0.jpg) no-repeat;
}

you would write:

$("#rangeTabAll").css("backgroundColor", "myNewValue");
$("#rangeTabAll").css("backgroundImage", "myNewValue");
$("#rangeTabAll").css("backgroundRepeat", "myNewValue");

or

$("#rangeTabAll").css("background-color", "myNewValue");
$("#rangeTabAll").css("background-image", "myNewValue");
$("#rangeTabAll").css("background-repeat", "myNewValue");
balexandre
Thanks alot. You're right. Sorry i cant accept more than one.
rism
That's a bug! We need to report to Jeff Atwood, ehehehe ;)
balexandre
+1  A: 

Why not define your alternate background as a CSS class and use $('#rangeTabAll').addClass('alternate') and $('#rangeTabAll').removeClass('alternate')?

eyelidlessness
that is just an opinion, should be in comments, that is not the question... right? --> Question is: "How do i read/write the background value using jQuery?"
balexandre
Um, no, it's another way of solving the problem.
eyelidlessness
Moreover, I'm promoting better practices by suggesting separating program logic from design.
eyelidlessness