Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
82
rated 0 times [  88] [ 6]  / answers: 1 / hits: 29034  / 9 Years ago, tue, february 10, 2015, 12:00:00

I would like to have a form button disabled, until a user clicks a checkbox.



This is the code i have (last part of the form)



<div class=checkbox>
<input id=check name=checkbox type=checkbox>
<label for=checkbox>
Some Text Here
</label>
</div>
<input type=submit name=anmelden class=button id=btncheck value=Send />


I tried the following



$('#check').click(function(){

if($(this).attr('checked') == false){
$('#btncheck').attr(disabled,disabled);
}
else
$('#btncheck').removeAttr('disabled');
});


But this is not working and I am not sure why. I don't know how I can disable the button (should be disabled also visually).



Solution (thanks to Rory McCrossan): http://jsfiddle.net/0hvtgveh/


More From » jquery

 Answers
12

Your logic is a little off, try this:



$('#check').change(function () {
$('#btncheck').prop(disabled, !this.checked);
}).change()


Updated fiddle



Note that the UI of the button does not update when disabled, however the disabled property does change. You would probably want to add some CSS styling to make it obvious that the button is disabled.



Also, I changed the code to use the change event instead of click to better cater for people who navigate using the keyboard.


[#67887] Saturday, February 7, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
byrondonavanc

Total Points: 675
Total Questions: 107
Total Answers: 105

Location: Peru
Member since Fri, Oct 14, 2022
2 Years ago
byrondonavanc questions
;