views:

139

answers:

3

Is there a possibility to access a i.e "background-image:url()" property inside an inline "style" attribute? In other words going layers "deeper" than attr('style') does.

I want to manipulate the url inside style="background-image:url(something);"

+2  A: 

Setting CSS rules

The easiest way to modify a CSS property on elements in a jQuery object, is the css function:

$("selector").css("background-image", "url(something)");

If you want to set several CSS properties in one go, there is an "overload" of the function that takes an object and uses each member of this object to override the related CSS property:

$("selector").css({
    "background-image" : "url(something)",
    "color" : "red"
});

Getting CSS rules

If you need to read the current value of some CSS property, use another "overload" of css:

var backgroundImg = $("selector").css("background-image");
Jørn Schou-Rode
Using the `css()` function with two parameters instead of an object saves characters ;)
Douwe Maan
@DouweM: I was just editing that in as you posted your comment :)
Jørn Schou-Rode
How didn't I think of that! Maybe to tired.Thank you very much!
Tobi
A: 

You could use regular expressions to find the actual content of the url() 'function', but wouldn't it be easier just to use the following?

$('selector').css('background-image', 'url(something.png)');
Douwe Maan
A: 

I want to manipulate the url inside style="background-image:url(something);"

You would use that style for an element right?

$("selector").css('background-image', 'url(me.jpg)');
Sarfraz
You still have to use `url()` around the path.
Douwe Maan