Clicking anywhere in a <li> to check a checkbox contained within it, without checking the checkbox itself becoming a problem.
Forked from Chris Smith's Pen Checking Checkboxes by Clicking Parent List Item.
A Pen by Dries Desmet on CodePen.
Clicking anywhere in a <li> to check a checkbox contained within it, without checking the checkbox itself becoming a problem.
Forked from Chris Smith's Pen Checking Checkboxes by Clicking Parent List Item.
A Pen by Dries Desmet on CodePen.
| <ul> | |
| <li><input type="checkbox"> Tyrannosaurus Rex<span><button>DETAILS</button></span></li> | |
| <li><input type="checkbox"> Stegasaurus</li> | |
| <li><input type="checkbox"> Pteradactyl</li> | |
| <li><input type="checkbox"> Triceratops</li> | |
| <li><input type="checkbox"> Diplodocus</li> | |
| </ul> | |
| <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> |
| $("li").click(function (e) { | |
| console.log("running li click event") | |
| var cb = $(this).find(":checkbox")[0]; | |
| console.log(e.target); | |
| if (e.target != cb) cb.checked = !cb.checked; | |
| $(this).toggleClass("selected", cb.checked); | |
| }); | |
| $("input:checkbox").click(function (e){ | |
| console.log(e.target); | |
| }); | |
| $("li span").click(function (e){ | |
| event.stopPropagation(); | |
| console.log(e.target); | |
| }); | |
| $("li button").click(function (e){ | |
| alert("bootstrap stuff happening...") | |
| }); |
| ul { | |
| list-style-type:none; | |
| margin:0; | |
| padding:0; | |
| } | |
| li { | |
| padding:5px 8px; | |
| } | |
| li:nth-child(even) { | |
| background:#ccc; | |
| } |