Search code examples
jqueryvariable-length

How do I use the answer of an .length object count in an if statement?


I want to have some dynamics in my creation and therefor need jQuery to count the times a div appears within a different div and based upon the answer executes a function; change css to be exact.

<div class="tiles">
  <div class="tile"></div>
  <div class="tile"></div>
  <div class="tile"></div>
</div> 

I want jQuery to tell me the amount of .tile within .tiles

$(function() {
    var countOfTiles = $('.tiles > .tile').length;

    if ($('.tiles > .tile').length = 2) {
        alert('no');
    } else if ($('.tiles > .tile').length = 3) {
        alert('yes');
}

However, it either shows 'unexpected identifier' or else the first statement is always true..

So how to?


Solution

  • You need to use == for comparisons. Your current code is trying to set the length property which is readonly, hence the error