Get and change CSS of some containers

1

I hope you are well, I tell you what I should do ...

I must do a function that detects me if one or many div within their styles have the property background-color: #ffffff; and in case they have it, change it by background-color: #000000; I can not think of any way to do it and this function It must be applied to all HTML.

They know some form or they could locate me on how to do it.

Thank you very much

    
asked by Jhojan Guerrero 28.12.2018 в 16:18
source

1 answer

3

In JQuery with the css() method, you can retrieve the style properties you need from an element, for example:

  

Update:

Within the click function, you must filter the div to condition using the query $ (this) and thus be able to select only those that meet the condition:

//Añado boton para que aprecies el cambio.

  $(".c").click(function(e){  
    $("div").filter(function(){
      var divColor = $(this).css("background-color");
      if (divColor === "rgb(255, 0, 0)") {

      $(this).css({"background-color": "blue"});

    }
    });  
  });
.hola, .ala {

  background-color: rgb(255, 0, 0);

}

.eje {

  background-color: purple;

}

.sys {

  background-color: #FF3;

}

div {

  margin: 10px;
  width: 40px;
  height: 40px;

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="hola"></div>
<div class="eje"></div>
<div class="ala"></div>
<div class="sys"></div>
<button class="c">Cambiar color</button>

This way you can see if any div contains the color you need to change and as you see, only those that are red will change.

Remember to express the values of the colors in RGB because the css property of JQuery will always return them like this, this means that if you have a color: red; JQuery it will return rgb(255, 0, 0) , if you have your color in HEX or only in CSS, make sure to convert it to RGB to condition it, example:

if (divColor === "rgb(255, 0, 0)"){...} //Si tu color era red, por ejemplo.
//Si no estas seguro igual puedes hacer esto:
if (divColor === "rgb(255, 0, 0)" || divColor === "red"){...}

I hope it's helpful, greetings!

    
answered by 28.12.2018 / 16:31
source