13

Is there a simple way to use jQuery to remove all background styles on a page? Specifically, need to remove background color and images.

Ian McIntyre Silber
  • 5,553
  • 13
  • 53
  • 76

3 Answers3

31

Real simple with jQuery...

$('*').css('background', 'transparent');

jsFiddle.

If you didn't have jQuery at your disposal...

var allElements = document.getElementsByTagName("*");

for (var i = 0, length = allElements.length; i < length; i++) {
    allElements[i].style.background = "none";
}

jsFiddle.

alex
  • 479,566
  • 201
  • 878
  • 984
1
$('*').css('background', 'transparent');
alex
  • 479,566
  • 201
  • 878
  • 984
Sharon
  • 11
  • 1
  • What are the advantages of using `'transparent'` over `'none'`? – alex Mar 27 '13 at 20:50
  • @alex -- Apparently `transparent` is the valid property assignment here, where `none` is not valid CSS2... see http://stackoverflow.com/questions/8739665/is-background-colornone-valid-css – Ian Campbell Jul 15 '13 at 19:48
  • @IanCampbell Interesting. `transparent` it is :) – alex Jul 16 '13 at 00:35
-1

HTML

<ul>
 <li class="one"><a href="#"></a></li>
        <li class="two"><a href="#"></a></li>
        <li class="three"><a href="#"></a></li>
</ul>

CSS

.bg1 { background: url(images/red.jpg) repeat-x; background-color: #6c0000; }
.bg2 { background: url(images/orange.jpg) repeat-x; background-color: #5A2A00; }
.bg3 { background: url(images/blue.jpg) repeat-x; background-color: #00345B; }

JS

$(document).ready(function(){ 

        $("li.one").click( function(){ $
  ("body").removeClass('bg2 , bg3').addClass("bg1");
 });

 $("li.two").click( function(){ $
  ("body").removeClass("bg1 , bg3").addClass("bg2");
 });

 $("li.three").click( function(){ $
  ("body").removeClass("bg1 , bg2").addClass("bg3");
 }); 

});
Josh
  • 8,082
  • 5
  • 43
  • 41
pooja
  • 2,334
  • 3
  • 16
  • 16