mercredi 4 février 2015

jquery loop function forever?

I am using the following jquery script which changes the colour of a checkbox to red when a user has clicked on it, and to black when it is unchecked.


The checkbox starts of black as it is unchecked, then when a user clicks in the checkbox it changes the background of the checkbox to red.


My problem is this, if a user checks the checkbox the first time this goes from black to red, then if they uncheck the checkbox it goes back to black. However for whatever reason if they want to re-check the checkbox it does not go back to red?


Is there a way in which I can make this function run on a loop? thanks in advance...



<script>
$(this).children("input").attr("checked");
$(document).ready(function() {

// assuming all checkboxes are unchecked at first
$("span[class='checkbox']").addClass("unchecked");

$(".checkbox").click(function(){
if($(this).children("input").attr("checked")){
// uncheck
$(this).children("input").attr({checked: ""});
$(this).removeClass("checked");
$(this).addClass("unchecked");
}else{
// check
$(this).children("input").attr({checked: "checked"});
$(this).removeClass("unchecked");
$(this).addClass("checked");
}


});
});
</script>




Aucun commentaire:

Enregistrer un commentaire