1

My js code:

$('div[aria-label="Asukohad"] input[type="checkbox"]').on("change", function() {
    var element = $(this);

    if(element.is(":checked")) {
        //todo
    } else {
        uncheckAll();
    }
});

function uncheckAll() {
    $('div[aria-label="Asukohad"] input[type="checkbox"]').attr('checked', false);
}

When I select some checkboxes and then unselect one of them, all the checkboxes get unchecked, which is great. But now when I select another checbox all the other checkboxes are also getting checked which where unchekced by jQuery. I got a feeling that I have to sync the checkboxes somehow when I do the unchecking with jQuery.

Checkboxes are located in post/page properties panel.

Video demo:https://vimeo.com/343601969

2 Answers 2

1

Can you please clarify your question? Are you attempting to make a checkbox that can clear all checkboxes and also recheck all checkboxes by checking that one checkbox?

Also a checked input would be written like this:

<input id="editor-post-taxonomies-hierarchical-term-48" class="editor-post-taxonomies__hierarchical-terms-input" type="checkbox" checked value="48" />

You can do this:

$('div[aria-label="Asukohad"] input[type="checkbox"]').on("change", function() {
var element = $(this);

if(element.is(":checked")) {
    // do nothing.
} else {
    uncheckAll();
}
});

function uncheckAll() {
    $('div[aria-label="Asukohad"] input[type="checkbox"]').removeProp('checked');
}
4
  • 1
    No, Im trying to make a checkbox that can clear all checkboxes. When rechecking only the current checkbox should get checked, not the ones that were unchecked with jQuery. My checked input does not look like you showed. Maybe thats relatead with this wierd behavior.
    – MulOnPomm
    Jun 21, 2019 at 9:58
  • 1
    your video is not working, can you give an explanation of the difference between checkboxes that were unchecked and what is meant by current? Jun 29, 2019 at 22:09
  • 1
    I updated the video link. vimeo.com/343601969. Video shows what is happening now. By current I mean that the checkbox I click should only get clicked. Not the ones which were unchecked previously by jquery.
    – MulOnPomm
    Jul 1, 2019 at 4:57
  • 1
    I updated my answer with something that should work for what you seem to be wanting it to do. Please mark as answer if it worked for you. Jul 7, 2019 at 2:20
0

I had the same question, so here you go.

To check all boxes:

jQuery('#select-button').on('click',function(){
    jQuery('.outer-div td label input[type="checkbox"]').prop('checked', true);
});

To uncheck all boxes:

jQuery('#select-button').on('click',function(){
    jQuery('.outer-div td label input[type="checkbox"]').removeProp('checked');
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.