The following code adds a checkbox and a label to the calling node.
My question lies in the label.click function. Whenever the label is clicked I want to change the state of the matching checkbox. What happens however is that the checkbox always ends up unchecked.
For debugging purposes I now always explicitly set it to checked.
When I step through the code with firebug I see that the checkbox gets checked and then, when leaving the function it gets unchecked again.
Any ideas?
jQuery.fn.AddEndOrStartWith = function(selected, id, action) {
var checkBox = $('<input type="checkbox"></input>');
checkBox.attr("id", id + action);
checkBox.addClass(action + "CheckBox");
checkBox.attr("for", id);
var label = $('<label></label>');
label.attr("for", id + action);
if (selected) {
checkBox.attr("checked", "checked");
label.addClass("lockerClosed");
} else {
label.addClass("lockerOpen");
}
$(this).append(label);
$(this).append(checkBox);
label.click(function() {
/*alert(checkBox.attr("checked"));*/
checkBox.attr("checked", "checked");
/*return true;*/
/*alert(checkBox.attr("checked"));*/
});
}