views:

31

answers:

2

I have a series of checkboxes, and everything works except their default behavior has been altered unintentionally so I can no longer check them which is odd since the reason I was using this bit of jquery was to highlight the li around them when they got checked in the first place.

Any ideas?

//Tag cloud
$(".tag-cloud li").toggle(
  function () {
    //$(this).filter(":checkbox").attr('checked', 'true');
    $(this).addClass("selected");
    return;
    //return $(this).filter(":checkbox").attr("checked", true);
  },
  function () {
    //$(this).filter(":checkbox").attr('checked', 'false');
    $(this).removeClass("selected");
    return;
    //$("#sortable").submit();
  }
);
+4  A: 

You can simplify it overall and hopefully eliminate the odd behavior by using a simple .click() event handler, like this:

$(".tag-cloud li").click(function() {
 var cb = $(this).find(":checkbox")[0];
 $(this).toggleClass("selected", cb.checked);
});

This has the benefit of working regardless of what state it was initially in, where as .toggle() will be off for pre-checked boxes.

If you want the <li> itself clickable, we need to be sure not to get in a loop or reverse the toggle when clicking the actual checkbox itself (if it's exposed), like this:

$(".tag-cloud li").click(function(e) {
 var cb = $(this).find(":checkbox")[0];
 //if the click wasn't from the checkbox already, toggle it
 if(e.target != cb) cb.checked = !cb.checked;
 $(this).toggleClass("selected", cb.checked);
});
Nick Craver
@nick, in your first snippet you need the `[0]` for the `cb` assignment..
Gaby
@Gaby - good catch, updated :)
Nick Craver
@nick, any insight on why the OP faces this issue ? why is the `checkbox` messing the actual click ?
Gaby
@Gaby - I'm not 100% sure, but the `.toggle()` being initial state dependent is one issue, combined with checking/unchecking the box on a `<li>` click which will *also* fire when the checkbox inside is clicking will cause all sorts of weird behavior, for example: clicking and the `.toggle()` functions immediately switching the check value back. It depends on the initial state as to *which* problem it is, but the event propagation's a problem either way :)
Nick Craver
@Nick, but the code to change the checkbox is in comments.. the issue persists even with just adding a class.. the `toggle` being on the `li` should have no effect on the `checkbox` itself..
Gaby
@Gaby - I think that's just interpretation of the question, I think the problem described is from the commented code and that's why it's being shown...I agree that strictly the uncommented code couldn't have the effect the OP describes, so I am somewhat assuming that it *has* to be the commented portions...since they would have the exact problem he describes (reversing the check as soon as it's done).
Nick Craver
@nick, i have tested the code.. the problem exists in the commented case... look at http://jsfiddle.net/4EkpF/
Gaby
@Gaby - Oh you're right, another reason not to use `.toggle()`, it calls `event.preventDefault()` on the `click` underneath: http://github.com/jquery/jquery/blob/master/src/event.js#L977 So yes I guess even as-is you'd still have issues. Don't use `.toggle()` unless it's *really* needed, state issues alone are just not friendly.
Nick Craver
@Nick, aha ! Nice info to know ... thanks.
Gaby
A: 

Try removing the return; lines.

Blair McMillan
that will not work @Blair..
Gaby
i added those after it wasn't working ;-)
holden