Why does a checkbox's "checked" attribute return the opposite of it's actual value on click event?

Posted by Kappers on Stack Overflow See other posts from Stack Overflow or by Kappers
Published on 2010-04-01T22:58:15Z Indexed on 2010/04/01 23:03 UTC
Read the original article Hit count: 325

I've got the following HTML

<li><input type="checkbox" /><label>This is the label!</label></li>

I bound a click event to the li, and let the click event bubble up to the li before doing anything else.

$('li').each(function(i){
  var item = $(this);
  var checkbox = $("input[type='checkbox']", item);

  item.bind('click', function(e){
    var isChecked = checkbox.is(':checked');
    console.log(isChecked);
    e.stopPropagation();
  });
});

Starting with an unchecked checkbox, when the click event fires, isChecked returns true when I click on the checkbox, but returns false when I click on the label or li. Does anyone know why?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JavaScript