vendredi 3 novembre 2017

Get checkbox status inside function that changes them

I'm using a series of checkboxes (using iCheck) in order to filter content. for example

  • All
  • PHP
  • HTML
  • Javascript
  • CSS

Every checkbox has a .filter class. All has .filter_all and the others .filter_spec

Here's the code:

$(".filter").on('ifClicked', function(){
    if($(this).hasClass("filter_all"))
    {
        if (!$(this).is(":checked"))
        {
            $(".filter_spec").iCheck("uncheck");
        } else
        {
            $(".filter_spec").iCheck("check");
        }
    } else
    {
        $(".filter_all").iCheck("uncheck");

        if($(this).is(":checked") && ($('.filter:checked').length == 1))
        {
            $(".filter_all").iCheck("check");
        }

    }

    str = "";
    $(".filter:checked").each(function(){
        str += $(this).attr("name")+' ';
    });
    alert(str);
});

By default only All is selected. If i click PHP, All will automatically deselect and if nothing is selected All will automatically select again.

Until now it's all working. The problem is I want to fire a function and get the updated status of the checkboxes. How can I do it? Note that I have to use !NOT when I check the status of All and .length == 1 because I don't have the updated value.

Thanks for your time!




Aucun commentaire:

Enregistrer un commentaire