Reputation: 59
It's doesn't work ( Why? Can anybody explain, please
function checkBox()
{
if($('.checkbox').prop('checked', true))
{
$('body').css('background','yellow');
}
else if ($('.checkbox').prop('checked', false))
$('body').css('background','blue');
}
Upvotes: 2
Views: 197
Reputation: 2242
FIDDLE: https://jsfiddle.net/7rpeL2gu/4/
Thats the answer HTML:
<input class="checkbox" type="checkbox" />
JS:
$(document).ready(function(){
$('.checkbox').on('click', function(){
checkBox( $(this) );
});
});
function checkBox($this){
if( $this.prop('checked') ){
$('body').css('background-color','yellow');}
else{
$('body').css('background-color','blue'); }
}
P.S. Your code does not work, because $(input).prop('checked', true)
- set input
to checked
, but $(input).prop('checked')
give you input.state
of checked.
P.P.S read this https://api.jquery.com/prop/
Upvotes: 0
Reputation: 15393
.is()
- Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.
if($('#checkbox').is(':checked')){
$('body').css('background','yellow');
}
else{
$('body').css('background','blue');
}
Upvotes: 1