Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select an element with it's background color is not working in jQuery

Please check my code. The condition for checking the background color are not working.

https://jsfiddle.net/oL7tdL22/1/

$(function(){

$(".testing").each(function(){
   if($(this).css("background-color")=="rgb(255,193,0)"){
     alert("found");
   }
   else{
     alert("not found");
   }
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="parent">
<div class="testing"  style="background-color:rgb(255,193,0)">
Test
</div>
</div>

<div class="parent">
<div class="testing"  style="background-color:rgb(220, 4, 81)">
Test
</div>
</div>


<div class="parent">
<div class="testing"  style="background-color:rgb(0, 186, 76)">
Test
</div>
</div>

When we are alert background color, it's working. But we cannot match the colors.

like image 260
Abilash Erikson Avatar asked Jul 06 '17 09:07

Abilash Erikson


People also ask

How can get background color value in jQuery?

click(function() { var color = $( this ). css( "background-color" ); $( "p" ). html( "That div is " + color + "." ); });

What is the correct jQuery code to set the background color of all elements?

What is the correct jQuery code to set the background color of all p elements to red? $("p"). style("background-color","red"); $("p").

How would you use jQuery to give an element a red background color?

add("textarea"). css( "background", "red" ); });


2 Answers

you need to give a space after each comma in rgb color code like rgb(255, 193, 0). Then it works.

$(function(){

$(".testing").each(function(){
   if($(this).css("background-color")=="rgb(255, 193, 0)"){
     alert("found");
   }
   else{
     alert("not found");
   }
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="parent">
<div class="testing"  style="background-color:rgb(255,193,0)">
Test
</div>
</div>

<div class="parent">
<div class="testing"  style="background-color:rgb(220, 4, 81)">
Test
</div>
</div>


<div class="parent">
<div class="testing"  style="background-color:rgb(0, 186, 76)">
Test
</div>
</div>
like image 77
Dij Avatar answered Oct 23 '22 12:10

Dij


You need to seperate the rgb values with a space, as JQuery parses the .css("background-color") value this way.

rgb(X, Y, Z)
      ▲  ▲

This is the correct code:

if($(this).css("background-color")=="rgb(255, 193, 0)"){
   alert("found");
}

Code snippet:

$(function(){
     $(".testing").each(function(){
        if($(this).css("background-color")=="rgb(255, 193, 0)")
          alert("found");
        else
          alert("not found");
     });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="parent">
    <div class="testing"  style="background-color:rgb(255,193,0)">Test</div>
</div>
<div class="parent">
    <div class="testing"  style="background-color:rgb(220, 4, 81)">Test</div>
</div>
<div class="parent">
    <div class="testing"  style="background-color:rgb(0, 186, 76)">Test</div>
</div>
like image 31
Koby Douek Avatar answered Oct 23 '22 14:10

Koby Douek