I am trying since this morning to create a code using jQuery that allows to enable/disable a submit button when I click on a checkbox. I am trying to write all this code inside a function that will be called on everyclick.
The problem is that it works fine on JSFiddle, but on my computer one time the button is enabled I cannot make it disabled again by unchecking all checkboxes.
Do you have any idea to resolve this issue?
Thank you in advence.
This is my code:
function activeBouton() {
var countChecked = function() {
var n = $( "input:checked" ).length;
if (n == 0) jQuery('button').prop('disabled', true);
else jQuery('button').prop('disabled', false);
};
countChecked();
$( "input[type=checkbox]" ).on( "click", countChecked );
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div onclick="activeBouton()">
<input type="checkbox" name="nom" value="1" /> Item un
<input type="checkbox" name="nom" value="2" /> Item deux
<input type="checkbox" name="nom" value="3" /> Item trois
</div>
<button id="activeBtn" disabled>Add</button>