jQuery question about .css()

Is there a way for me to create something like this in jQuery during RUNTIME, i.e., ON THE FLY:

.someStyle {
    color: yellow;
    background: red;
    border: 1px solid blue;
}

Open in new window


and then be able to reference the whole set of styles as .addClass(".someStyle")?

In other words, I don't want to have to do it this way: $("#someElement").css("color", "yellow"), and so on.

Thanks.
elepilAsked:
Who is Participating?
 
Brian TaoSenior Business Solutions ConsultantCommented:
Ah, sorry for the miss-reading.  To create the style on the fly and apply it to an element, here's what you need (use .css() method):
var someStyle={
    "color": "yellow",
    "background": "red",
    "border": "1px solid blue"
}
$("#someElement").css(someStyle);

Open in new window

0
 
Brian TaoSenior Business Solutions ConsultantCommented:
Yes, just use $("#someElement").addClass("someStyle");
Please note when you use addClass the parameter is the class name and not a class selector (means no need for the dot)
0
 
elepilAuthor Commented:
Brian, thanks for responding, but I don't think you read my question carefully.

Notice I bold-faced RUNTIME and ON THE FLY.

I wanted to know if there was a way to still use addClass("someStyle") that somehow represents CSS styles created entirely within jQuery and not something defined in an external style sheet or within a <style> tag.
0
 
elepilAuthor Commented:
Exactly what I was looking for, thanks much, Brian!
0
 
Brian TaoSenior Business Solutions ConsultantCommented:
Glad to help.  Thanks for the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.