I'd like to create a function to check and uncheck checkboxes when we click on the previous span element, it works one time but it doesn't work a second time... I don't understand...
My html code :
<form class="form" action="#" method="post">
<label>Input radio</label>
<span>Text</span><input type="checkbox">
<span>Text</span><input type="checkbox">
<span>Text</span><input type="checkbox">
<label>Input checkbox</label>
<span>Text</span><input type="radio" name="radio">
<span>Text</span><input type="radio" name="radio">
</form>
My js code :
$('form.form').find('span').on('click', function(){
$this = $(this);
if( $(this).next().attr('type') == 'checkbox' ){
if( $this.next().is(':checked') ){
$this.next().attr('checked', false);
}else{
$this.next().attr('checked', true);
}
}else{
$this.next().attr('checked', true);
}
});
Update 1
URL : http://jsfiddle.net/tonymx227/bvPsq/
Anthony