Jquery checkbox with li

I have a number of checkboxes and everything works, except that their default behavior was unintentionally changed, so I can no longer check them, which is odd, because the reason I used this jquery bit was to highlight them around them when they are first checked.

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();
  }
);
+3
source share
2 answers

You can simplify it overall and hopefully eliminate odd behavior using a simple .click()event handler, for example:

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

, , .toggle() .

, <li> , , , ( ), :

$(".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);
});
+8

return;.

0

Source: https://habr.com/ru/post/1772344/


All Articles