14

I have some CSS in string form, e.g.

border: solid 1px #0000ff; background-color: #ffff00;

and I want to apply that CSS to a <div>. But all of the examples for using jQuery to apply CSS involve using the css method, and to go that route, I'd have to split the CSS string by semicolons (;) to retrieve property-value pairs, then by (:) to retrieve the property and value, and do

$('myDiv').css(property, value);

Is there a way to apply the CSS directly, from its string form?

I worry that such a naive parsing of the CSS (semicolons and colons) will fail if CSS values can contain those characters, e.g. in url sub-values.

1
  • 1
    you could try using .attr() Commented Feb 12, 2013 at 10:11

8 Answers 8

26

You can retrieve the existing style attribute and then set a new one:

var target = $("#target");
target.attr("style", target.attr("style") + "; " + yourString);

Live Example | Source

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

2 Comments

Thanks. I can't believe I didn't think of this in the time I spent typing up that question.
It works, it just leads to some unnice css, if you dynamically apply attributes like this, as they change. (will be a concatenation of multiple times "yourString", where ofc. the latest occurence of every prop will win)
4

Assuming there are no inline styles set on the element using the style attribute, the following will work:

$("#foo").attr('style', 'border: solid 1px #0000ff; background-color: #ffff00;');

Example fiddle

Note that this is not an ideal solution though. A much better method would be to put the required styles in a class and use addClass() instead.

2 Comments

You can always just contact the style strings and keep any inline styles.
@DeclanCook indeed, TJs answer is more complete than mine for that reason
4

First get your div element using a selector:

var div = document.getElementById("myDiv"); // myDiv is the id right?

Then get the current css:

var css = div.getAttribute("style");

If it's null then set it to the new style. Otherwise append the new style:

var style = 'border: solid 1px #0000ff; background-color: #ffff00;';
if (css === null) css = style;
else css += style;

Finally update the style of the div:

div.setAttribute("style", css);

That's it. See the demo: http://jsfiddle.net/xUWzw/

Comments

3

There is another way of adding string based styles. If below is the style string -

    border: solid 1px #0000ff; background-color: #ffff00;

Then

    var div = document.getElementById('myDiv');
    div.style.cssText = 'border: solid 1px #0000ff; background-color: #ffff00;';

Fiddle: https://jsfiddle.net/eja0zea4/

Comments

1

You can add the css string directly to the div into the style attribute using jQuery .attr() function, but a better way would be to assign a class to your div with jQuery .addClass() function, and adding the css properties to the class.

Comments

1

Just use the jQuery's function .attr('style', 'somthing...'),It's very easy to use and you don't have to think about fault tolerance .

Comments

0

Why not creating a CSS class and just using the addClass removeClass method from jQuery

.myClass {
   border: solid 1px #0000ff; 
   background-color: #ffff00;
}

then

$(div).addClass('myClass');

2 Comments

The CSS is entered by the user, after the page loads. Consider, for example, a tutorial page where the user enters bits of CSS, and a target on the page styles dynamically. How would I apply the CSS class, then?
You can post the new user style and create a dynamic CSS file with php script then include it on the fly: $('head').append('<link rel="stylesheet" href="style2.css" type="text/css" />'); or just do everything on client side by injecting inline style: $('<style>.newClass { color: red; }</style>').appendTo('body');
0

you can retrieve it as an object

$('myDiv').css({property:"value",property:"value",property:"value"});

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.