Marcin Kusuah
Marcin Kusuah

Reputation: 119

Overwrite color and background with css

I have to change color and background for whole elements in the site. I make it like this:

var color_text = ["initial","#00FF21","#fff"];
var color_background = ["initial","#f00","#000"];
var ile = 1;

$('.kontrast').click(function () {
  $('#main').css("color", color_text[ile]);
  $('#main').css("background", color_background[ile]);
  $('a#main').css("color", color_text[ile]);
  ile = (ile+1) % 3;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="kontrast" href="#">Contrast</a>


<div id="main">
  <h3>A little bit of text </h3>
  <p>z A little bit of text .</p>
  <p><strong>A little bit of text</strong></p>
  <p>A little bit of text</p>
  <p><a href="#">Download .pdf</a></p>
</div>

In this case I can't change it for a elements . It doesn't work with h3 elements in my site too. How can I overwrite it for totally all elements? I've tried to do that like this

$('a#main').css("color", color_text[ile]);

But it doesn't work

Upvotes: 0

Views: 93

Answers (2)

Suhail Keyjani
Suhail Keyjani

Reputation: 488

you can change jquery selector to "main *"

var color_text = ["initial","#00FF21","#fff"];
var color_background = ["initial","#f00","#000"];
var ile = 1;

$('.kontrast').click(function () {

  $('#main *').css("color", color_text[ile]);
  $('#main *').css("background", color_background[ile]);
  $('a#main').css("color", color_text[ile]);
  ile = (ile+1) % 3;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<a class="kontrast" href="#">Contrast</a>


<div id="main">
  <h3>A little bit of text </h3>
  <p>z A little bit of text .</p>
  <p><strong>A little bit of text</strong></p>
  <p>A little bit of text</p>
  <p><a href="#">Download .pdf</a></p>
</div>

Upvotes: 2

Danny T
Danny T

Reputation: 112

if you change:

$('#main').css("background", color_background[ile]);

to this:

$('body').css("background", color_background[ile]);

it will work.

Upvotes: 0

Related Questions