11

How can i remove background-image from element style. I don't want to set it to none or 0. I want to remove it completely. it's conflicting with moz-linear-gradient which is defined in another class.

<div style="background-image:url(http://domain.com/1.jpg); width:100px" class="mozgradient"></div>
Pinkie
  • 10,126
  • 22
  • 78
  • 124
  • 4
    We need clarification here. Remove it 'how'? In a text editor? Via CSS? Using Javascript? – DA. Nov 07 '11 at 03:51
  • Remove it via jQuery or javascript so div looks like `
    `
    – Pinkie Nov 07 '11 at 03:54
  • @Pinkie As far as I am aware every element has background-image: none; by default. Removing it using JavaScript will just reset it to the default value as it has no overriding property. So unfortunately you can't get rid of it. – joshnh Nov 07 '11 at 03:55
  • @joshuanhibbert Yes i understand that, but i don't want it in styles. If i have it as background:none and i have another class with background gradient. it will not work. I want to remove background-image from style completely. – Pinkie Nov 07 '11 at 04:04

6 Answers6

12

Have you tried:

$(".mozgradient").css("background", "");

Setting the background to an empty string should remove the inline style such that class and other stylesheet settings take effect, but it shouldn't affect other styles that were set inline like width. (In this case removing it from all elements with the mozgradient class prevents the conflict you are talking about.)

From the jQuery .css() doco:

Setting the value of a style property to an empty string — e.g. $('#mydiv').css('color', '') — removes that property from an element if it has already been directly applied, whether in the HTML style attribute, through jQuery's .css() method, or through direct DOM manipulation of the style property.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • nnnnnn - that's good info! This last part of the documentation seems to indicate it's exactly what @pinkie is looking for "It does not, however, remove a style that has been applied with a CSS rule in a stylesheet or – DA. Nov 07 '11 at 04:18
  • 3
    Note that this isn't just a jQuery thing: you should be able to achieve exactly the same thing with `yourElement.style.background = "";` – nnnnnn Nov 07 '11 at 04:24
  • `$(".mozgradient").css("background", "");` works not in all browsers :-/ i think better is `$(".mozgradient").css("background", "none");` – Bob Sep 21 '13 at 18:21
  • @Bob - No, that won't do what Pinkie wants. `.css("background","")` will _remove_ the inline background style from the element(s) in question while still allowing other background styles from the style sheet to take effect, but your suggestion of `.css("background", "none")` _sets_ an inline style and thus overrides the stylesheet. – nnnnnn Sep 21 '13 at 23:59
2

I'm not entirely sure what you are after, but I'm making an assumption that you have the inline style on the div tag, and via CSS, you're trying to over-ride that.

The only option here is the !important flag in your css file. So in your css file you'd have this:

.mozgradient {background-image(whatever) !important}

UPDATE:

I see now that you are trying to do it via jQuery. Try this:

var divWidth = $yourDiv.css('width');

$yourDiv.removeAttr('style').css('width', divWidth);

Though note that that would only work provided 'width' is the only css inline style you want to preserve. If there could be any inline style in there, you'd have to resort to either the CSS option above of using !important or using jQuery, grab the entire style attribute and get dirty with REGEX to parse out any background-image styles specifically.

UPDATE II:

OK, based on the comments, this is getting tricky. The challenge is that there may be any number of inline styles being applied via the style attribute (ugh! I feel your pain!) and we only want to clear out the background-image so that we can let the external CSS handle it.

Here's another option:

// create a div and attach the class to it
$testDiv = $('<div>').class('mozgradient').css('display','none');

// stick it in the DOM
$('body').append($testDiv);

// now cache any background image information
var bgndImg = $testDiv.css('background-image');

// get rid of the test div you want now that we have the info we need
$testDiv.destroy();

// now that we have the background-image information 
// from the external CSS file, we can re-apply it
// to any other element on the page with that class
// name to over-ride the inline style
$('.mozgradient').css('background-image',bgndImg);

Clunky, but I think it'd work.

DA.
  • 39,848
  • 49
  • 150
  • 213
  • I can't use this. I do not know how many styles i have. It's dynamically generated. I only need to look into background-image. – Pinkie Nov 07 '11 at 03:59
  • The jQuery part. Your creating a variable divwidth. I may have 20 different styles dynamically generated. I can't assign variables like this. – Pinkie Nov 07 '11 at 04:02
  • Right. Well, as I state, I think you're only option at that point is to resort to text parsing via REGEX. Grab the STYLE attribute as a string, look for the background-image part, remove it, then put it back. Alas, REGEX always gives me headaches so I won't be much specific help for you. I'd try and figure out if you can implement the `!important` flag on the external CSS file instead. Might be a saner solution. – DA. Nov 07 '11 at 04:04
  • The question is i want to remove background-image from style. I don't want it there. i can't set it to none because it will conflict with background-image from another class – Pinkie Nov 07 '11 at 04:06
  • @Pinkie I updated my answer with a 3rd option. See if that might work. – DA. Nov 07 '11 at 04:11
2

If you want to dynamically remove background image, then use $("selector").css(); function in jquery or inn CSS apply background:none See more at: http://jsfiddle.net/creativegala/um3Ez/

Kangkan
  • 15,267
  • 10
  • 70
  • 113
Arun Kumar
  • 61
  • 3
0

The best way to handle this is at the HTML level or in whatever server-side script you use to create the HTML. Just have it not print the style attribute. Once it's there, it takes highest priority in the CSS cascade so it over-rides anything else in class or id definitions.

The second best option is to over-ride it after load using JavaScript. You can't remove it but you can replace it with whatever the class is trying to pass as long as you can add an id tag to it..

.mozgradient { background-image:url(http://domain.com/2.jpg); } /* will not work */
#fixedelement { background-image:url(http://domain.com/2.jpg); } /* will not work */
<div style="background-image:url(http://domain.com/1.jpg); width:100px" class="mozgradient" id="fixedelement"></div>
<script type="text/javascript">
fixedelement.style.backgroundImage = 'url(http://domain.com/2.jpg)'; /* will work */
</script>
Juniper Jones
  • 779
  • 6
  • 10
0

A workaround would be to 'reset' the property. In this case, I think the following should do the trick:

div[class="mozgradient"] { background-image: none !important; }

iglvzx
  • 498
  • 1
  • 8
  • 22
  • I think this is the reverse of what the OP is looking for. They don't want the inline style so that the background-image defined in the .mozgradient class in the external css file is used instead. – DA. Nov 07 '11 at 04:20
0
$(div you use).removeAttr('style')

this should remove the style

$(div you use).attr('style', 'width: 100px');

and then should add the specified style with just height

not sure if it what you want

COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
  • Alas, that won't work as the OP doesn't know which styles may exist inline (sounds like there could be any number of them) – DA. Nov 07 '11 at 04:15