title: jQuery show/hide bar depending on css class
body: <p>I have a dynamic number of images with a delete button. the first time i click a delete button the image is selected and a bar at the top appears. if i click the same button again, the image is unselected and the top bar should disappear again. the same should happen with multiple selections /removals of selection.</p>
  
  <p>I tried to achieve this by adding a class on click (selected) and removing it when clicked again.</p>
  
  <p>I then count the the length of the class (how many exist) and if equals 0, the top bar should disappear again.</p>
  
  <p>the console.log shows me that it always counts 0.</p>
  
  <p>what am i missing?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var bar = $('#bar');
  var deleteBtn = $('.delete');
  var selected = $('.selected');
  var selection = 0;
  
  
  
  deleteBtn.click(function() {
  
    if ($(this).hasClass("selected")) {
      $(this).removeClass("selected");
    } else {
      $(this).addClass("selected");
    }
  
    var selection = selected.length;
  
    if (bar.hasClass("hide")) {
      bar.addClass("show");
    } else {
      if (selection === 0) {
        bar.removeClass("barShowY");
      }
    }
    console.log(selection + " selected images");
  
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>#bar {
    height: 100px;
    width: 100%;
    background-color: firebrick;
    color: white;
  }
  
  .hide {
    margin-top: -100px;
  }
  
  .show {
    margin-top: 0;
  }
  
  .delete {
    width: 50px;
    height: 50px;
    background: yellow;
    cursor: pointer;
  }
  
  .selected {
    border-bottom: 3px solid red;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;div id="bar" class="hide"&gt;delete selected images?&lt;/div&gt;
  
  &lt;div class="delete"&gt;delete&lt;/div&gt;
  &lt;img src="https://apollo2.dl.playstation.net/cdn/UP2131/CUSA01736_00/Ll2fJdBTPQylZO0Z4JQ9rtsIJsojwBxn.png" alt=""&gt;
  
  &lt;div class="delete"&gt;delete&lt;/div&gt;
  &lt;img src="https://apollo2.dl.playstation.net/cdn/UP2131/CUSA01736_00/lYG2Mgzz878Z8rU3yvlx9MwmR5dhDsyy.png" alt=""&gt;
  
  &lt;div class="delete"&gt;delete&lt;/div&gt;
  &lt;img src="https://apollo2.dl.playstation.net/cdn/UP2131/CUSA01736_00/Zp2VXCJxT764bqDPyyIy0Yf2ZRFHzr3g.png" alt=""&gt;</code></pre>
  </div>
  </div>
  </p>