Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable button when checkboxes selected

I have multiple checkboxes and a submit button that is initially disabled. When checking a box the button is enabled and when unchecking, the button is disabled again.

If have multiple checkboxes selected but uncheck one, the button becomes disabled even though I have selected other checkboxes. How can I fix this issue?

<script type="text/javascript"> 
$(function() {
    $(".checkbox").click(function() {
      $(".delete").attr("disabled", !this.checked);
    });
});
</script>

HTML

<input type="checkbox" name="msg[]" value="32" class="checkbox" />
<input type="checkbox" name="msg[]" value="44" class="checkbox" />
<input type="checkbox" name="msg[]" value="26" class="checkbox" />

<button type="submit" class="delete" disabled="disabled">Delete</button>
like image 410
CyberJunkie Avatar asked Sep 02 '11 18:09

CyberJunkie


2 Answers

$(function() {
    $(".checkbox").click(function(){
        $('.delete').prop('disabled',$('input.checkbox:checked').length == 0);
    });
});

Demo: http://jsfiddle.net/AlienWebguy/3U364/

like image 77
AlienWebguy Avatar answered Nov 10 '22 02:11

AlienWebguy


Try this where I am basically checking if all the checkboxes are not checked then disable the button.

$(function() {
    $(".checkbox").click(function() {
      $(".delete").attr("disabled", !$(".checkbox:checked").length);
    });
});
like image 26
ShankarSangoli Avatar answered Nov 10 '22 01:11

ShankarSangoli