stopPropagationの例、checkbox を td セルのクリックでも制御

JavaScript stopPropagationの例、メモ。

テーブルタグに配置したcheckbox を td セルのクリックでも制御


class="sample" の 配下の tableで、、1列目に checkbox ある場合、、

$(".sample td").click(function(){
   if ($(this).parent().children("td:nth-child(1)").children("input[type='checkbox']").prop("checked")){
      $(this).parent().children("td:nth-child(1)").children("input[type='checkbox']").prop("checked", false);
   }else{
      $(this).parent().children("td:nth-child(1)").children("input[type='checkbox']").prop("checked", true);
   }
});
$(".sample td input[type='checkbox']").click(function(ev){
   ev.stopPropagation();
});