title: Javascript if(condition) then addclass
body: <p>I'm trying to get a class added on when a div is inside a certain parent div. </p>
  
  <pre><code>&lt;div class="parent1"&gt;
      &lt;div class="child"&gt;
        Content
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>.parent1 only exists on one page, while .child exists on others as well as this one.</p>
  
  <p>So when .child is everywhere else, its color is red, but when it's inside .parent1 I want its color to be blue.</p>
  
  <p>Here's what I'm using.</p>
  
  <pre><code>if ($('.child').parents('.parent1').length == 1) {
      .addClass('.new-class');
  }
  </code></pre>
  
  <p>I'm having no success with this. Can anyone help?</p>