There is html markup. For clarity, colored blocks to correctly explain your question.

enter image description here

Here is the jquery code:

$(document).ready(function(){ if($('div').is('[class*=icon-my]')){ $('div.icon_block').removeClass('icon_block').addClass('icon_block_my_big').css("background-color","yellow"); } }); 

But with this code, the condition for all blocks (green, orange) works. Question: how can I alter my code so that the condition only works on green blocks?

  • Make a demo on jsfiddle with markup. - zb '
  • Divide ul or li into the green and orange classes and work with them. - bemulima

1 answer 1

 $(document).ready(function() { $('div[class*=icon-my]') .closest('ul') .find('.icon_block') .removeClass('icon_block') .addClass('icon_block_my_big') .css("background-color", "yellow"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul> <li> <div class="icon_block">1</div> </li> <li> <div class="icon icon-my-05">2</div> </li> </ul> <ul> <li> <div class="icon_block">3</div> </li> <li> <div class="icon icon-my-06">4</div> </li> </ul> <ul> <li> <div class="icon_block">1</div> </li> <li> <div class="icon icon-iba">2</div> </li> </ul> 

  • VenZell, thank you very much, everything works. The question is closed. - Maxim Gerasimov
  • one
    @Maxim Gerasimov, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - VenZell