3

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();
  }
);

2 Answers 2

8

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);
});
Sign up to request clarification or add additional context in comments.

6 Comments

@nick, any insight on why the OP faces this issue ? why is the checkbox messing the actual click ?
@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, 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 - 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).
@Gaby - Oh you're right, another reason not to use .toggle(), it calls event.preventDefault() on the click underneath: 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.
|
0

Try removing the return; lines.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.