tags:

views:

28

answers:

1

I want to extract the background from this CSS and then use it to set the page's background-color:

.ui-widget-header { border: 1px solid #4297d7; background: #5c9ccc url(images/ui-bg_gloss-wave_55_5c9ccc_500x100.png) 50% 50% repeat-x; color: #ffffff; font-weight: bold; }

The jQuery I have so far:

$(function() {
    var color = $(".ui-widget-header").css("background");
    //var color = "black"; // works fine when uncommented
    $(document.body).css("background-color", color);
});

I know I'm not far off since the commented out line works and sets the page's background to be black. I think the problem is that background in .ui-widget-header isn't a simple color and includes an image.

If that's indeed the problem, how can I extract just the color (#5c9ccc)? Plain old Javascript would be fine too, if that's simpler.

More background (no pun intended): I'm using jQuery-UI Themeroller and want to set the page background based on the theme a user chooses.

+3  A: 

Try grabbing background-color instead:

var color = $(".ui-widget-header").css("background-color");
moff
Nine seconds separate the same answer from yourself and Pekka; *damn* this place is good! ...and *fast*. =)
David Thomas
That it is. :) I just had it slightly wrong (camelCased), which is why I deleted my answer.
Pekka
Thanks, that did it. Since I didn't see 'background-color' in that CSS snippet, I relied on just 'background'. This proves I finally need to learn CSS and can't just wing it anymore.Bonus question: how can I accomplish this if no elements exist in '.ui-widget-header'? Right now I have to include a dummy <div class="ui-widget-header>.
Slack
Bob: I really don't know. I experimented a bit with creating an element dynamically in jQuery, assigning a class to it and then checking for the background color via jQuery.css, though it didn't work. I'll investigate it a bit.
moff