toggleClass messing something up.

Posted by Mathias Nielsen on Stack Overflow See other posts from Stack Overflow or by Mathias Nielsen
Published on 2010-04-22T14:11:48Z Indexed on 2010/04/22 14:13 UTC
Read the original article Hit count: 267

Filed under:
|
|

Hi I am using playing around with the Pretty-checkboxes-plugin and have a problem. The plugin can be seen here http://aaronweyenberg.com/90/pretty-checkboxes-with-jquery

As you can see, this uses two links to select and deselect. I want it to only use one link and then change the class to make it work the other way the next time it is clicked.

It should be simple enough. I have been trying to get it working with toggleClass.

I simply put in the toggleClass statement so the code looks like so:

$(document).ready(function() {

/* see if anything is previously checked and reflect that in the view*/ $(".checklist input:checked").parent().addClass("selected");

/* handle the user selections */ $(".checklist .checkbox-select").click( function(event) { event.preventDefault(); $(this).parent().addClass("selected"); $(this).parent().find(":checkbox").attr("checked","checked"); $(this).toggleClass("checkbox-select checkbox-deselect");

}

);

$(".checklist .checkbox-deselect").click( function(event) { event.preventDefault(); $(this).parent().removeClass("selected"); $(this).parent().find(":checkbox").removeAttr("checked"); $(this).toggleClass("checkbox-select checkbox-deselect");

}

);

});

This works to some extend. The classes toggle just fine, but the rest of the code only works the first time it is run. All subsequent clicks just toggle the class og the link and nothing else.

Does anyone have any idea why this is?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about toggleclass