0

I have HTML form with checkboxes "delete" array and one checkbox "togglechbx" to toggle all checkboxes to check/uncheck. So when none of delete[] is checked, "delete-selected-btn" has to be disabled and checkbox "togglechbx" has to be uncheck automatically.

The javascript below does the trick, but i have it combined from different stackoverflow examples. That's why it looks so ugly and messy. Could you guys take a look, if there is some way to make it shorter and cleaner? Thank you in advance.

<form id="show-images">
<input id="togglechbx" type="checkbox" onClick="toggle(this);" />
<input type="checkbox" class="delete-img" name="delete[]" value="1"/>
<input type="checkbox" class="delete-img" name="delete[]" value="2"/>
<input type="checkbox" class="delete-img" name="delete[]" value="3"/>
<button id="delete-selected-btn" type="submit" disabled="disabled">Delete selected</button></form>
<script>
        $('.delete-img').change(function(){
         var arrlenght = $("#show-images input:checkbox:checked").length;
         if ( arrlenght > 0){
         if ( arrlenght == 1 && document.getElementById("togglechbx").checked) {
            $("#togglechbx").prop("checked", false);
            $('#delete-selected-btn').attr("disabled", "disabled");
            return false;
         }
         // any one from delete is checked
            $('#delete-selected-btn').removeAttr("disabled");
         }else{
         // none is checked
              $('#delete-selected-btn').attr("disabled", "disabled");
       }});

    function toggle(source) {
               checkboxes = document.getElementsByName('delete[]');
               for(var i=0, n=checkboxes.length; i<n; i++) {
                checkboxes[i].checked = source.checked;
                if ($("#show-images input:checkbox:checked").length > 0){
                        $('#delete-selected-btn').removeAttr("disabled");
                }else{
                // none is checked
                        $('#delete-selected-btn').attr("disabled", "disabled");
                 }
              }
    }
</script>

3 Answers 3

1

Here's what I came up with:

(function() {
  var $delBtn = $("#delete-selected-btn"),
      $delCBs = $("input.delete-img").click(function() {
          var checkedCount = $delCBs.filter(":checked").length;
          $delBtn.prop("disabled", checkedCount === 0);
          if (checkedCount === 0)
            $toggleCB.prop("checked", false);
          else if (checkedCount == $delCBs.length)
            $toggleCB.prop("checked", true);
       }),
       $toggleCB = $("#togglechbx").click(function() {
          $delCBs.prop("checked", this.checked);
          $delBtn.prop("disabled", !this.checked);
       });
})();

Jawdroppingly amazing demo: http://jsfiddle.net/Wfdy3/3/

What I'm doing above is whenever any of the delete-img checkboxes is clicked I find out how many of them are checked and then disable the delete-selected-btn button if that number is zero. Then the if/else unchecks the master toggle checkbox if none of the others is checked, or checks the master toggle if all of the others are checked - it doesn't change the state of the master checkbox if some but not all are checked. I'm not clear from your description if that's how you wanted the auto-checking of the master checkbox to work - if you only wanted it checked if all the others were checked you could replace the four-line if/else structure with $toggleCB.prop("checked", checkedCount === $delCBs.length); as shown here: http://jsfiddle.net/Wfdy3/4/

Then in the click handler for the master toggle checkbox I simply set all of the other checkboxes to the same state as the master, and enable or disable the delete-selected-btn button as appropriate.

Note that the anonymous function is just to keep the variables out of the global scope, and I'm using click handlers rather than change handlers because (I vaguely recall that) in some browsers when a checkbox is changed via the keyboard the change event doesn't occur until the checkbox loses focus, but the click event works with mouse or keyboard.

Note also that .prop() is the appropriate jQuery method to use to set the disabled state, not attr() and removeAttr() (I assume you're using a new enough version of jQuery to be able to use .prop() given that you used it yourself in one place.)

Sign up to request clarification or add additional context in comments.

2 Comments

This is what i was dreaming of. Thank u very much
Of course, master checkbox must act exactly like here in your script, not like in the one i've made.
1

You can try something like this:

$(function() {
    $("#show-images>.delete-img").change(function() {
        if ($("#show-images>.delete-img:checked").length) {
            $("#delete-selected-btn").removeAttr("disabled");
        } else {
            $("#delete-selected-btn").attr("disabled", "disabled");
        }

        $("#togglechbx").prop("checked", $("#show-images>.delete-img").length == $("#show-images>.delete-img:checked").length);
    });

    $("#togglechbx").change(function() {
        $("#show-images>.delete-img").each(function (i, e) {
            $(e).prop("checked", $("#togglechbx").prop("checked"));
        });
        $("#show-images>.delete-img").change();
    });
});

5 Comments

Why do you need to change the input's name attribute? The current name is legal, or those elements already have a common class.
True, I just don't like using brackets in names :) Updated code using classes.
Yeah, I'm not a big fan of brackets in names either, but I assume the OP is using PHP.
jsfiddle.net/7uqYc on toggle all the button is not getting active, only on checking single checkboxes
Forgot to change one of the [name='delete'] to .delete-img, it's fixed.
0
<form id="show-images">
<div class="toggle">
<input id="togglechbx" type="checkbox" onClick="toggle(this);" />
<input type="checkbox" class="delete-img" name="delete[]" value="1"/>
<input type="checkbox" class="delete-img" name="delete[]" value="2"/>
<input type="checkbox" class="delete-img" name="delete[]" value="3"/>
<button id="delete-selected-btn" type="submit" >Delete selected</button>
</div>
</form>

javascript

$("#show-images") .submit(function(){

$(".toggle") .toggle();

$('#delete-selected-btn').attr('disabled', '');

return false;
});

1 Comment

Well that's definitely shorter than the OP's code, but it does something completely different, so...

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.