How to toggle checkbox when parent element is clicked.
I have an <input type="checkbox"> whose immediate parent is an <a> tag. The <a> is significantly larger than the <input>.
I want the checkbox to toggle on/off whether the <a> is clicked or the checkbox itself. I also want to store the value of the checkbox after it changes.
This sounds simple but I'm having trouble with the event bubbling (as in, I don't understand it).
Here is my current code.
HTML:
- <a href="javascript:void(0)">Some text <input type="checkbox"></a>
JS:
- $('a').click(function() {
- $(this).find('input').click();
- });
- $('input').click(function(e) {
- e.stopPropagation(); //?
- //store the value of the checkbox ...
- });