0

On my website, the "Sort By Category" works OK when your checking the checkboxes it appends each of each category and hides the rest of the other one.

What I want to do is be able to show all if category items if none of the checkboxes are check, if you go on the site and click on 2 categorys and uncheck them you will notice it will be empty.

The CSS item for the posts is the class = "rp-item".

This is what my category sort looks like right now,

function categorySort() {      
  $('#CategorySort').attr('firstclick', true);

  $('.cat-sort-toggle').click(function() {
    $('.cat-sort-bar').toggle(500);
  });

  $('.cat-sort-check').change(function() {
    if ( $('#CategorySort').attr('firstclick') == 'true') {
      $('.rp-item').css('display', 'none');
      $('#CategorySort').attr('firstclick', false);
    }

    $objectString = '.' + $(this).attr('showclass');

    if ( $(this).is(':checked') ) {
      $($objectString).show(500);
    } else {
      $($objectString).hide(500);
    }
  });
}

/* the code below was the one i tried to fix the issue,
what it did is it kinda worked but everytime i click the
2nd time on any of the check boxes or just switch
checkboxes it brings everything back instead of only
the posts from the checkbox. Thanks! */ 

else{
  $('.rp-item').css('display', 'inline-block');
  $('#CategorySort').attr('firstclick', true);
} 
Spencer D
  • 3,376
  • 2
  • 27
  • 43

2 Answers2

0

you can check if none of checkbox checked by

if($('input[type="checkbox"]:checked').length < 1){
   alert('No one checked');
}

Note : This code is just a basic .. check for classes and Ids yourself and update this code in the event you want

Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
  • Hey Man , Thanks Unfortunetely it did not work when i click on it it doesnt load up its posts and if i uncheck it shows nothing. I just want it to show all rp-items if all of them are unchecked , and if i check one to continue to sort. Right now it sorts but when none is checked it just shows nothing. Thanks! – Gabriel Macedo Nov 23 '15 at 16:34
0

$("input[type='checkbox' class='rp-item']:checked") gives you a list of the checkboxes that are checked. You can then strap it inside an if:

if($("input[type='checkbox' class='rp-item']:checked").length > 0){
    // Do Something
}

And write your application logic inside of it.

"jQuery-less" approach:

var checkboxes = document.getElementsByClassName("rp-item");
var nothingChecked = true;
for(var i = 0; i < checkboxes.length; i++) {
    if(checkboxes[i].checked == false)
        continue;
    else
        nothingChecked = false;
}
if(nothingChecked == true) {
    // Do Something
}
guilherme.oc97
  • 431
  • 1
  • 3
  • 13
  • Hey Man , Thanks Unfortunetely it did not work when i click on it it doesnt load up its posts and if i uncheck it shows nothing. I just want it to show all rp-items if all of them are unchecked , and if i check one to continue to sort. Right now it sorts but when none is checked it just shows nothing. Thanks! – Gabriel Macedo Nov 23 '15 at 16:34
  • Hey check the else statement that i tried but it dint work any suggestions? – Gabriel Macedo Nov 23 '15 at 17:43