0

i have some divs with same class please see this

<div class="parent" style="display:none">
        <div class="class-1" style="color:rgb(198, 172, 0);">hii</div>
        <div class="class-1" style="color:rgb(134, 122, 36);">hii</div>
        <div class="class-1" style="color:rgb(251, 206, 146);">hii</div>
        <div class="class-1" style="color:rgb(249, 70, 28);;">hii</div>
</div>

<div class="div2">text</div>

How to perform trigger click for class-1 which style colour is rgb(134, 122, 36)?

2
  • 2
    how can you click when the div is hidden? Commented Dec 22, 2016 at 9:08
  • there are other jquery and other html part . I just written the basic for what i need . Commented Dec 22, 2016 at 9:09

3 Answers 3

2

you can use jQuery's trigger() with the help of the jQuery attribute equals selector

$(".class-1[style='color:rgb(198, 172, 0);']").on('click', function(){
	alert('works');
});

$(".class-1[style='color:rgb(198, 172, 0);']").trigger('click');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent" style="display:none">
        <div class="class-1" style="color:rgb(198, 172, 0);">hii</div>
        <div class="class-1" style="color:rgb(134, 122, 36);">hii</div>
        <div class="class-1" style="color:rgb(251, 206, 146);">hii</div>
        <div class="class-1" style="color:rgb(249, 70, 28);;">hii</div>
</div>

<div class="div2">text</div>

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

1 Comment

1

You can loop over the elements with a given class and then compare their css color attribute and trigger a click if it matches your requirement.

$(document).ready(function(){
  $('.class-1').on('click', function(){
    alert($(this).css('color'));
  })
  $('.class-1').each(function(index, item) {
    console.log($(item).css('color') == 'rgb(134, 122, 36)');
    if($(item).css('color') == 'rgb(134, 122, 36)') {
      $(item).trigger("click");
    }
  })
  
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent" >
    <div class="class-1" style="color:rgb(198, 172, 0);">hii</div>
    <div class="class-1" style="color:rgb(134, 122, 36);">hii</div>
    <div class="class-1" style="color:rgb(251, 206, 146);">hii</div>
    <div class="class-1" style="color:rgb(249, 70, 28);;">hii</div>

</div>

<div class="div2">text</div>

2 Comments

There is not much difference, you can just change the color attribute value to test.
1

You also can do something like this.

$(function(){
    $(".class-1[style='color:rgb(134, 122, 36);']").on('click', function(){
        alert('red');
    });

    $('.class-1').each(function (i, el) {
        if($(el).attr('style') == 'color:rgb(134, 122, 36);') {
        $(el).trigger('click')
      }
    })
})

1 Comment

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.