1

I have 4 buttons in this code. When selecting the "all" button, I would like the other 3 buttons to uncheck and refresh. Here is what I have:

$('div.regions').find('input:checked').each(function () {
    var id = this.id;
    var now = moment();
    if (id == "ALL")
    {
        if (item.Date >= now){ data.push(item); }

    }
    else if (id == item.Region)
    {
        if (item.Date >= now){ data.push(item); }
    }
});

Here is a jsfiddle of the code: http://jsfiddle.net/ZHvYH/ Can anyone help?

2
  • What do you mean with refresh? And where is your attempt to uncheck? Commented Nov 21, 2013 at 23:30
  • By refresh I mean return the button UI to its unchecked state. I've tried $( '#SCV' ).prop( "checked", false ).checkboxradio( "refresh" ); and $('.SCV').prop('checked', false); with no success. Commented Nov 21, 2013 at 23:41

3 Answers 3

3

Updated the JSFiddle - http://jsfiddle.net/ZHvYH/5/

var checkboxes = $('div.regions input[type="checkbox"]:not([id="ALL"])');

$('div.regions input[type="checkbox"]').on({
    'change': function(){
        var checkbox = $(this);

        if(checkbox.attr('id') == 'ALL'){
            checkboxes.prop('checked',checkbox.prop('checked')).checkboxradio('refresh');
        }
        else {
            $('#ALL').prop('checked',(checkboxes.length == $('div.regions input[type="checkbox"]:checked:not([id="ALL"])').length)).checkboxradio('refresh');
        }
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

This solution will ensure that when every option is "checked" the ALL option will automatically be checked as well. +1
If I want to uncheck the all category once another button has been selected, how do I code that?
@Bacon2305 You could just do $('#ALL').prop('checked',false).checkboxradio('refresh');
1

When you check #ALL, remove the checked attribute from all the other checkboxes, and then refresh them.

var checkboxes = $('div.regions input[type="checkbox"]:not([id="ALL"])');

$('#ALL').on({
    'click': function(){
        if (this.checked) {
            checkboxes.removeAttr('checked').checkboxradio("refresh");
        }
    }
});

http://jsfiddle.net/ZHvYH/6/

3 Comments

ummmm I don't think your fiddle works(I'm using chrome). And You do not account for unchecking them if the all is pressed again. jsfiddle.net/ZHvYH/12
When the ALL checkbox is selected, the others deselect. That's literally all the question asks for.
Thank you! Works very well! Now if I want to uncheck the "all" button once different button is selected, how would this been done? Thanks!
0
 if (id == "ALL")
    {
        if (item.Date >= now){ data.push(item); }
    $('input[type=checkbox]').each(function () {
         this.checked ? this.attr('checked', false) : null   ;
    });

    }

Maybe you need a bit of tweaking..i don't tested, i write down as i imagined.

hope this helps..

1 Comment

Thank you for the response. Unfortunately it didn't work in the jsfiddle.

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.