0

on my way to the shortest code on urff.

i have an object, where i make a feature detection. i then pass the result to a jquery.css() command. but i do have some other css rules, that i need to apply, too. i cant put them into the object, cause these rules differ from time to time. so i now pass two jquery.css() commands, but im trying to make one out of it, just for the sexyness..

var myObject  = (Modernizr.csstransforms ? {transform: 'translateX(123px)'} : {left: 123});
$("#elem").css(myObject).css({ sexyness: 0 });

while i try to make it something like this:

$("#elem").css(myObject, { sexyness: "100%" });

am i missing something, or is this as good as it gets?

2
  • You should avoid using css as much as possible. If you need it for dynamic values, so be it, but for static styles you should be using addClass, removeClass, and toggleClass. Commented May 7, 2013 at 17:17
  • thanks. its dynamic, the code above is just an example.. Commented May 7, 2013 at 17:17

4 Answers 4

4

You can use $.extend() to merge those two css property objects

$("#elem").css($.extend({}, myObject, { sexyness: "100%" }));
Sign up to request clarification or add additional context in comments.

Comments

2

You need "glue" objects and use new object for set style. Looks like this:

fixed

$.extend(myObject, { sexyness: "100%" });
$("#elem").css(myObject);

Comments

2
$("#elem").css($.extend({}, myObject, { sexyness: "100%" }));

jQuery.extend

Comments

0

Wrong :( Misread the API jQuery 1.9 allows you to use arrays of properties, so you can do:

var myObject  = (Modernizr.csstransforms ? 
                          ["transform: translateX(123px)"] : 
                          ["left: 123"]);
$("#elem").css(myObject.add("sexyness: 80"))

Lets cheat and use JSON.parse, which lets us simply concatenate strings

var myObject  = (Modernizr.csstransforms ? 
                 'transform: "translateX(123px),"'} : 
                 'left: 123,');
$("#elem").css(JSON.parse(myObject + 'sexyness : 80'))

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.