jQuery has() not working as intended

Viewed 95

Maybe I'm understanding it wrong but shouldn't this has() work in this situation? What am I doing wrong?

  $("#count").on("click", function(){
    $(".item").each(function( index, element ){
      $(element).text( index + 1 );

      /*** this line does nothing ****/
      $(element).has(".special").css("background", "black");

      if( index + 1  == 4  || index + 1  == 8 ){
         $(this).css("background", "#A5C663");
      }
    })
  })

HTML:

<div id="grid">
    <div class="item"></div>
    <div class="item special"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item special"></div>
    <div class="item"></div>
  </div>

This is not working either:

$(this).has(".special").css("background", "black"); 

Or:

$(".item).has(".special").css("background", "black"); 
1 Answers

Replace the has with is.

  • is is about this set of elements
  • has is about the descendants
Related