title: Automatically check parent when child is checked
body: <p>When any checkbox with class .child-term is checked, I automatically need to check the preceding checkbox with class .parent-term.</p>
  
  <p>I only want to check the preceding .parent-term class not all the checkboxes that have .parent-class. Same goes for the .child-term class. Any checkbox with .child-term only affects the .parent-term above.</p>
  
  <p>If the .parent-term class was checked from a .sub-term checkbox the .parent-term can remain checked when all the associated .sub-term boxes are unchecked.</p>
  
  <p>I have tried various solutions, but I can't figure it out.</p>
  
  <p>I started a <a href="https://jsfiddle.net/6y132wy8/1/" rel="nofollow">jsfiddle</a>.</p>
  
  <pre><code>&lt;ul class="cpt-terms-checkbox"&gt;
      &lt;li class="parent-term church" id="category-church"&gt;&lt;input id="church"
      name="church" type="checkbox" value="church"&gt;Church&lt;/li&gt;
  
      &lt;li&gt;&lt;small&gt;Sub Categories:&lt;/small&gt;&lt;/li&gt;
  
      &lt;li class="child-term elder" id="category-elder"&gt;&lt;input id="elder"
      name="elder" type="checkbox" value="elder"&gt;Elder&lt;/li&gt;
      &lt;li class="child-term interim-pastor" id="category-interim-pastor"&gt;
      &lt;input id="interim-pastor" name="interim-pastor" type="checkbox" value=
      "interim-pastor"&gt;Interim Pastor&lt;/li&gt;
  
      &lt;li class="parent-term law-firm" id="category-law-firm"&gt;&lt;input id=
      "law-firm" name="law-firm" type="checkbox" value="law-firm"&gt;Law
      Firm&lt;/li&gt;
  
      &lt;li&gt;&lt;small&gt;Sub Categories:&lt;/small&gt;&lt;/li&gt;
  
      &lt;li class="child-term attorney" id="category-attorney"&gt;&lt;input id=
      "attorney" name="attorney" type="checkbox" value=
      "attorney"&gt;Attorney&lt;/li&gt;
      &lt;li class="child-term attorney" id="category-attorney"&gt;&lt;input id=
      "attorney" name="attorney" type="checkbox" value=
      "attorney"&gt;Paralegal&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>Adding to Anoop Joshi's solution . . . I changed .parent() to .parents() so it would move up higher and for those who may run this in Wordpress I wrapped it in an anonymous function.</p>
  
  <pre><code>(function($) {
      $(".child-term :input").change(function() {
          if (this.checked) {
              $(this).parents().prevAll(".parent-term:first").find("input").prop("checked", true);
          }
      });
  })( jQuery );   
  </code></pre>