0

I want to make a filter which works with a checkbox. When the checkbox is checked it should show some elements and when its not it should hide it.

This is the code I've got so far but I cant get it to work properly. Anyone who can help me out?

The html what got to hide/show

<li class="verwijder" class="kleding"><img class="aanbieding" src="images/jack.png"><span id="timer"></span></li>

The checkbox

<input id="checkboxeten" type="checkbox" name="filter" value="eten"><br>

Javascript

 $(document).ready(function(){
  if (document.getElementById("checkboxeten").checked = true) {
        $( ".kleding" ).show();
    } else {
        $( ".kleding" ).hide();
    }
 });
1
  • you're testing the checkbox when the document is loaded, not every time they click it. Commented Oct 30, 2014 at 12:42

3 Answers 3

1

You'll need an event handler for that

$('#checkboxeten').on('change', function() {
    $(".kleding").toggle(this.checked);
});

And you can't have more that one class attribute

<li class="verwijder kleding">
Sign up to request clarification or add additional context in comments.

Comments

0

You need to compare using == not =.

Should be:

if (document.getElementById("checkboxeten").checked == true) {

Or:

if (document.getElementById("checkboxeten").checked) {

= is used for assignment. == is used for comparison.

Also you will need an onchange event handler for your checkbox. You should fire this logic not only on page load but also when the checkbox is changed I assume. Or on a button click or some kind of event.

1 Comment

Thanks for the tip! it was not exactly what I was looking for but I learned of it atleast :)
0
  1. Replace <li class="verwijder" class="kleding"> with <li class="verwijder kleding">
  2. Using my updated jquery code.
  3. Here is working example (http://jsfiddle.net/yqkLpn6s/2/) FIDDLE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.