5

I have an element like:

<div style="background-color: #ffffff; background-image: url(image.jpg);></div>

The background properties are being set using jquery like so:

$('div').css('background-color', '#ffffff');

Once the various properties have been set I want to get them all out as a shorthand declaration. I was hoping that just by doing:

var background = $('div').css('background');

would work however it doesnt seem to.

Anybody got a solution to this?

The only thing I can come up with is doing string concatenation such as:

var background = $('div').css('background-color') + ' ' + $('div').css('background-image');

however this will be very messy and rquire a lot of checks as not all background variables are always set, such as position etc.

any thoughts how this can be done would be greatly appreciated!

3
  • $('div').css('background') works for me. Commented Jan 10, 2014 at 15:48
  • @hsz from the jQuery docs: "Retrieval of shorthand CSS properties (e.g., margin, background, border), although functional with some browsers, is not guaranteed" Commented Jan 10, 2014 at 15:50
  • well im running the latest version of firefox and it returns an empty string for me. Commented Jan 10, 2014 at 15:53

3 Answers 3

4

You can pass an array to the .css() method, and it'll return you an array object with the values of all the properties you asked for.

var backgroundStuff = [
  "background-color",
  "background-position",
  "background-repeat",
  // ...
];

var props = $(whatever).css( backgroundStuff );
alert(props["background-color"]); // whatever the background color is

This is a fairly new feature (version 1.9).

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

5 Comments

setting the properties on the element is not the problem, its getting them out in shorthand when they are explicitly declared that doesnt work
@MikeOram Right - this is about getting the properties out. The array you get back has each of the style property values in the same order as the input array.
ah sorry i misread your answer first time around. what if i dont no the properties that are set, i just want all the ones that are present?
@MikeOram First: I was wrong; the return value is an object with property names that correspond to the style property names; the values are the property values. It might depend on the browser what happens with unset properties; in Firefox, you seem to get the default value when you ask for a property that's not explicitly set in the on-element "style" or via CSS. (I think; it's tricky to tell for sure.)
Yes this is what I found too. Trouble s this is unreliable cross browser. Thank you for your suggestion however I have decided to go a different route.
1

Seeing as there does not appear to be a reliable cross browser solution to this, I have done the following to get around the problem.

I have set up an object like so:

var backgroundDefaults = {
    color: "#ffffff",
    image: "none",
    repeat: "repeat",
    position: "top left"
}

When the user changes one background property, such as the background colour, I use the object to set all the defaults on the element. By doing this it ensures that all the values that can be changed are always set and so I can reliably get them off the element using:

var background = $('div').css('background-color') + ' ' + $('div').css('background-image');

as mentioned in the original question.

Comments

-1

To mix with Pointy answer, you can use regexp on the style attribute like this :

var attr = $('div').attr('style')
var theRegExp = (attr.match(/(?![\s;])*(background[\-a-z]*)/g)); //Will give an array of backgrounds properties

console.log($('div').css(Array.prototype.slice.call(theRegExp))) //Will give an object of properties

Fiddle : http://jsfiddle.net/hLdZH/

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.