4

I can't seem to get this to work. What I want to do is have jQuery add / remove (or toggle) a class which has display:none on it

jQuery

  <script type ="text/javascript">

    //Event Triggers
    $("#cbVitamins").click(function(evt){
      $("#products h2.product-type[data-type=vitamin]").parent().addClass("hideItem");
    });
  </script>

CSS

  <style>
    .hideItem {
      display:none;
    }
  </style>

HTML Button to hook event onto

      <div>
        <span>Show: </span>
        <input id="cbVitamins" type="checkbox" checked="checked"/>
        <label for="cbVitamins">Vitamins</label>
      </div>

HTML → add .hideItem class to the li element

<li class="product-item" data-prod_id="V-C6614">
    <img class="product-image" src="images/products/vitamin-c.jpg" alt="Vitamin C - Product Photo">
    <h2 class="product-name" data-type="vitamin">Vitamin C</h2>
</li>

what its supposed to do:

enter image description here

3
  • use .toggleClass() Commented Aug 9, 2017 at 3:02
  • Or, if display:none is all it does, even toggle() Commented Aug 9, 2017 at 3:03
  • 2
    Your h2.product-type selector doesn't match the class on the h2 element. It should be h2.product-name. Commented Aug 9, 2017 at 3:06

4 Answers 4

4

The problem is that you're targeting the class product-type with your jQuery, when you're actually using the class product-name in your HTML.

You can toggle visibility with the .toggle() method -- you don't need to create a separate class:

//Event Triggers
$("#cbVitamins").click(function(evt) {
  $("h2.product-name[data-type=vitamin]").parent().toggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
  <span>Show: </span>
  <input id="cbVitamins" type="checkbox" checked="checked" />
  <label for="cbVitamins">Vitamins</label>
</div>

<li class="product-item" data-prod_id="V-C6614">
  <img class="product-image" src="images/products/vitamin-c.jpg" alt="Vitamin C - Product Photo">
  <h2 class="product-name" data-type="vitamin">Vitamin C</h2>
</li>

Hope this helps! :)

Sign up to request clarification or add additional context in comments.

3 Comments

thanks so much :) . Didn't realize I had typed in type instead of name that works as well. By the way, doesn't parents() target all parent classes up until html element? But in this instance, parents() is only target the next level up for me, which is what I want though. I thought only parentsUntil() does this? or am i thinking of parent() vs parents()?
Are you sure that parents() is only targeting the next level up for you? In both your example and my answer, we're using the singular parent(). I've just double-checked, and using parents() in my answer code does indeed toggle all visible elements. Don't forget that parentsUntil() is exclusive, and you can always chain parent() if need be; element.parent().parent() is perfectly valid.
never thought of parent().parent(). There's lots of other parent classes enclosing the data I had (but omitted), but I read the jQuery documentation so now I understand the distinction between parent() and parents(). thanks for info
2

you use of $("#products... in jquery.i can't see element with this id in html code.

Change code like this:

$('#cbVitamins').click(function(){
    $('.product-item').toggleClass('hideItem');
})

Note:I prefer use of change event.

$(document).ready(function(){
  $('#cbVitamins').click(function(){
    $('.product-item').toggleClass('hideItem');
  })
})
.hideItem {
  display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<div>
  <span>Show: </span>
  <input id="cbVitamins" type="checkbox" checked="checked"/>
  <label for="cbVitamins">Vitamins</label>
</div>
<li class="product-item" data-prod_id="V-C6614">
  <img class="product-image" src="images/products/vitamin-c.jpg" alt="Vitamin C - Product Photo">
  <h2 class="product-name" data-type="vitamin">Vitamin C</h2>
</li>

1 Comment

thanks this was helpful :)
1

You can use jquery hide() and show() methods.

$('.clickButton').click(function() {
    $('.element').hide();
});

$('.clickButton').click(function() {
    $('.element').show();
});

Hope thats help.

3 Comments

You can, but why would that work if .addClass("hideItem") didn't work?
Does toggle() without any parameter inside of that essentially do both hide() and show() or is different?
Yes it does indeed.
1

Toggle class using jquery

$("#cbVitamins").on('change', function() {
  $(".product-name").toggleClass("hide");
});
.hide {display: none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div>
  <span>Show: </span>
  <input id="cbVitamins" type="checkbox" checked="checked" />
  <label for="cbVitamins">Vitamins</label>
</div>

<li class="product-item" data-prod_id="V-C6614">
  <img class="product-image" src="images/products/vitamin-c.jpg" alt="Vitamin C - Product Photo">
  <h2 class="product-name" data-type="vitamin">Vitamin C</h2>
</li>

1 Comment

does change event just encompass more things than just click when describing event handler?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.