1

i have some div's [up to 1000] whose class name are same and different colours . please see the following html

     <div class="class-1" style="background-color:red; display:none;">1</div>
     <div class="class-1" style="background-color:black; display:none;">2</div>
     <div class="class-1" style="background-color:rgb(0,12,34); display:none;">3</div>
     ...
     <div class="class-1" style="background-color:rgb(5,12,65); display:none;">1000</div>

Here how can i trigger the click of class1 whose background color is black ? I check with filter function but i didn't get the correct result

 $(".class-1").filter(function(){
       var match="black";
   return ( $(".class-1").css('background-color') == match );
}).trigger("click");

Please help.

1
  • Oh man this does not seem like a good way to do this, if possible you should assign the styles to different classes and query using $("className").click(function(){}); ? Commented Jan 15, 2017 at 8:32

3 Answers 3

1

If you compare the below. It should work

 var match = "rgb(0, 0, 0)";

Here is CodePen: http://codepen.io/rachitgulati26/pen/MJjaBp.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you please attach the code here , instead of writting in codepen . Thank you .
also, how can i add extra class to the selected one ? for example i need to add a class named selected to the one who have background color is black
0

You might have forgotten to write a closing quote on your code on the second line.

Try changing it to var match="black"; and see if that helps.

The jQuery function .css() works that way and should work.

See https://api.jquery.com/css/ for more information, but based on the first example they give it is possible to retrieve your colour string that way.

Comments

0

I would suggest you use data tags. HTML

<div data-color="black">black</div>

JQUERY

$('[data-color="black"]').html('black found');

1 Comment

sorry , i cannot rewrite the html structure .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.