steelcityamir
steelcityamir

Reputation: 1218

Enable Button When Any Checkbox Values Change

I have a page with multiple checkboxes in varying states (checked or unchecked) . I'm trying to enable a button as soon as any of the checkbox values change state.

Here is my code so far but it doesn't seem to work properly yet:

var checkboxes = $("input[type='checkbox']");
   	
checkboxes.change(function(){
    $('#saveChanges').prop('enabled', true);
});

Upvotes: 0

Views: 545

Answers (3)

Shashank Shah
Shashank Shah

Reputation: 2167

Try,

<input class="myCheckBox" type="checkbox" value="true">
<input class="myCheckBox" type="checkbox" value="true">
<button type="submit" id="confirmButton">BUTTON</button>

var checkboxes = $('.myCheckBox');

checkboxes.on('change', function () 
{
    $('#confirmButton').prop('disabled', !checkboxes.filter(':checked').length);
}).trigger('change');

Upvotes: 0

Shashank Shah
Shashank Shah

Reputation: 2167

Try, where submit button would be..

<input type="submit" value="Do thing" disabled>
var checkboxes = $("input[type='checkbox']"),
    submitButt = $("input[type='submit']");
checkboxes.click(function() {
    submitButt.attr("disabled", !checkboxes.is(":checked"));
});

Upvotes: 1

Jobelle
Jobelle

Reputation: 2834

<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>

<script>

    $(document).ready(function () {


        $("input[type='checkbox']").change(function () {

            $('#saveChanges').prop('disabled', false);

        });

 });  
</script>
</head>
<body>


  <input type="checkbox" value="1" />
<input type="checkbox" value="1" />
<input type="checkbox" value="1" />
<button id="saveChanges" disabled>Save</button>

</body>
</html>

Upvotes: 2

Related Questions