I have a group of checkboxes with custom design in a div. Checkbox should work when the parent div is clicked. I have used td example in the fiddle for simple reference.
There are the 2 thing I am trying to achieve
- Check/Uncheck the checkbox when the parent element is clicked
- Display the selected checkbox value under the Result heading
First one is working fine. But for displaying(toggle) the selected checkbox value works only when clicked directly on checkbox/label. But I need this is work even when clicked on the parent element.
Here is what I have so far
//Checkbox selection
jQuery("td input[type='checkbox'],td label").on('click',
function(e){
e.stopPropagation();
});
// Check/uncheck checkboxes clicking on its parent div
jQuery("td").click(function(){
var arr = jQuery(".chkbox:checked").map(function() { return jQuery(this).prev().html(); }).get();
var chk = jQuery(this).find("input[type='checkbox']");
if(chk.is(":checked") == false)
{
chk.prop("checked", true);
}
else
{
chk.prop("checked", false);
}
});
//Displayes the selected checkbox text
jQuery(function() {
jQuery(".chkbox").change(function() {
var arr = jQuery(".chkbox:checked").map(function() { return $(this).prev().html(); }).get();
jQuery("#myDiv").html(arr.join(','));
});
});
2
Answers
This should do the trick:
Of course you could combine these 2 functions into one
Hope this might help.