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!