jQuery/Javascript: Click event on a checkbox and the 'checked' attribute.
Posted
by b. e. hollenbeck
on Stack Overflow
See other posts from Stack Overflow
or by b. e. hollenbeck
Published on 2010-05-07T04:30:52Z
Indexed on
2010/05/07
4:38 UTC
Read the original article
Hit count: 336
The code:
$('input.media-checkbox').live('click', function(e){
e.preventDefault();
var that = $(this);
if (that.attr('checked') == 'checked'){
var m = that.attr('media');
var mid = 'verify_' + m;
that.parents('div.state-container').find('ul.' + mid).remove();
that.attr('checked', false);
} else {
var url = AJAX_URL;
$.ajax({
type: 'GET',
url: url,
dataType: 'html',
success: function(data){
that.parents('li').siblings('li.verification').children('div.media-verification').append(data).fadeIn(500);
that.attr('checked', 'checked');
}
});
}
return false;
});
I am ajaxing in a form, then firing the click event on relevant checkboxes to ajax in another partial if necessary. The form is inserted nicely, and the click events are fired, checking the boxes that need to be checked and firing the second ajax, since the checked
attribute of the checkbox was initially false
.
What's curdling my cheese is if I UNCHECK one of those boxes. Despite e.preventDefault()
, the checked
attribute is set to false
BEFORE the test, so the if
statement always executes the else
statement. I've also tried this with $.is(':checked')
, so I'm completely baffled.
It appears that unchecked -> checked state reads the original state, but checked -> unchecked doesn't. Any help?
© Stack Overflow or respective owner