I have a form like this:
<form action='' onsubmit='void(0)' class="mainform">
<label class="form3"><input type="checkbox"> One a fancy cross-browser styled checkbox</label>
<label class="form3"><input type="checkbox"> two a fancy cross-browser styled checkbox</label>
<label class="form3"><input type="checkbox"> Three a fancy cross-browser styled checkbox</label>
<label class="form3"><input type="checkbox"> Four a fancy cross-browser styled checkbox</label>
<label class="form3"><input type="checkbox"> Five a fancy cross-browser styled checkbox</label>
<label class="form3"><input type="checkbox"> Six a fancy cross-browser styled checkbox</label>
<label class="form3"><input type="checkbox"> Seven a fancy cross-browser styled checkbox</label>
<label class="form3"><input type="checkbox"> eight a fancy cross-browser styled checkbox</label>
<label class="form3"><input type="checkbox"> 9 a fancy cross-browser styled checkbox</label>
<label class="form3"><input type="checkbox"> 10 a fancy cross-browser styled checkbox</label>
<label class="form3"><input type="checkbox"> 11 a fancy cross-browser styled checkbox</label>
<label class="form3"><input type="checkbox"> 12 a fancy cross-browser styled checkbox</label>
<label class="form3"><input type="checkbox"> 13 a fancy cross-browser styled checkbox</label>
<label class="form3"><input type="checkbox"> 14 a fancy cross-browser styled checkbox</label>
</form>
Now, I want to do this. When the user checks or unchecks a checkbox, I want to add/remove class to the label so that I can show different coloured text when teh checkbox is selected and when it is not.
I am trying to do it liek this:
$(document).ready(function(){
$('.form3').each(function(i,e) {
if(checklist[i] == "")
{
$(this).find('input[type=checkbox]').attr('checked', false);
$(this).appendTo($('form'));
$(this).find('input[type=checkbox]').change(function() {
$(this).closest('label').addClass("checkselected");
});
$(this).closest('label').removeClass("checkselected");
}
else
{
$(this).find('input[type=checkbox]').attr('checked', true);
$(this).find('input[type=checkbox]').change(function() {
$(this).closest('label').removeClass("checkselected");
});
$(this).closest('label').addClass("checkselected");
}
});
});
Now I know that this maynot be the right way to do it because I am doing this inside the "$('.form3').each(function(i,e)"
This makes it work but only once. How can I make it work even with mulltiple clicks to the same checkbox.