Reputation: 10476
Looking at this jsFiddle, my checkbox is never getting the checked
attribute. This is a VERY simple test, and yet I cannot get the alert to show true
.
<input type="checkbox" id="checky" />
$(document).ready(function() {
$("#checky").click(function() {
alert($(this).is("checked"));
});
});
Why does the alert show false
in both cases? And why does the checked
attribute never get set onto the checkbox? What am I doing wrong here?
Upvotes: 0
Views: 532
Reputation: 3282
$(function() {
//checkbox
$("#terms").click(function(){
//if this...
//alert("this")...
if($("#terms").is(':checked'))
{
alert("im checked");
}
});
//button
$("#buttonID").click(function(e){
if(!$("#terms").is(':checked'))
{
alert("you did not check the agree to terms...");
e.preventDefault();
}
});
}
You can use this it works for me
Upvotes: 1
Reputation: 23600
It's actually: alert($(this).is(":checked"));
Demo: http://jsfiddle.net/AdFEf/
Upvotes: 5
Reputation: 318342
It's actually :
$(this).is(":checked")
And binding the change event makes it work even if it's checked with the keyboard:
$(document).ready(function() {
$("#checky").on('change', function() {
alert($(this).is(":checked"));
});
});
Upvotes: 8