Search code examples
javascriptjqueryhtmlparent-childtoggleclass

Toggle class in closest parent


So I have this jquery that i need to make it work. But through all the parents() and children(), I can't get to where I want.

My objective is when I click in the button, the class icon-cross, toggle to icon-tick.

(In this snippet bellow, I made an example to explain better. In this case I want to change the color of the rectangle)

$(function() {
  $(".addOpt").click(function(e) {
    $(this).text(function(i, text) {
      return text === "Add" ? "Remove" : "Add";
    });
    $(this).toggleClass("btn-primary btn-remove");
    
    
    //the problem is the following line:
    $(this).closest(".quoteCompare").children(".quoteCompareInside").p.toggleClass("icon-tick icon-cross");
    
    
    e.preventDefault();
  });
})
.icon-cross {
  width: 50px;
  height: 10px;
  background-color: green;
}
.icon-tick {
  width: 50px;
  height: 10px;
  background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row ">
  <div class="medium-6 xsmall-12 small-12 column quoteCompare  ">
    <div class="quoteCompareInside quoteStandard sameHeight2">
      <p class="icon-cross"></p>
      <p class="title noMarginBottom">10€</p>
    </div>
  </div>
  <div class="medium-6 xsmall-12 small-12 column quoteCompare  ">
    <div class="quoteCompareInside quoteStandard sameHeight2">
      <div class="form-row valign">
        <button type="button" class="btn btn-primary buttonOpt addOpt">Add</button>
      </div>
    </div>
  </div>
</div>


Solution

  • You should take p as selector, not as property:

    $(this).closest(".quoteCompare").children(".quoteCompareInside").find('p').toggleClass("icon-tick icon-cross");
    

    If you want to get only 1st p element you can use eq method:

    $(this).closest(".quoteCompare").children(".quoteCompareInside").find('p').eq(0).toggleClass("icon-tick icon-cross");
    

    Edit:

    Note that closest quoteCompare will return second div with classes medium-6 xsmall-12 small-12 column quoteCompare, so you probably also should use prev() in your code:

    $(this).closest(".quoteCompare").prev().children(".quoteCompareInside").find('p').eq(0).toggleClass("icon-tick icon-cross");
    

    $(function() {
      $(".addOpt").click(function(e) {
        $(this).text(function(i, text) {
          return text === "Add" ? "Remove" : "Add";
        });
        $(this).toggleClass("btn-primary btn-remove");
        
        
        //the problem is the following line:
        $(this).closest(".quoteCompare").prev().children(".quoteCompareInside").find('p').eq(0).toggleClass("icon-tick icon-cross");
        
        e.preventDefault();
      });
    })
    .icon-cross {
      width: 50px;
      height: 10px;
      background-color: green;
    }
    .icon-tick {
      width: 50px;
      height: 10px;
      background-color: red;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="row ">
      <div class="medium-6 xsmall-12 small-12 column quoteCompare  ">
        <div class="quoteCompareInside quoteStandard sameHeight2">
          <p class="icon-cross"></p>
          <p class="title noMarginBottom">10€</p>
        </div>
      </div>
      <div class="medium-6 xsmall-12 small-12 column quoteCompare  ">
        <div class="quoteCompareInside quoteStandard sameHeight2">
          <div class="form-row valign">
            <button type="button" class="btn btn-primary buttonOpt addOpt">Add</button>
          </div>
        </div>
      </div>
    </div>