I would like to trigger a click on a checkbox when the text next to the checkbox is clicked ("Finance department" or "Sales department" in the snippet).
I have tried with both jquery and javascript but any of those two seem to work:
$(prevsibling).children().click();
$(prevsibling).children().trigger("click");
Please find attached a snippet with the code.
Any clue ?
Thanks a lot
$(document).mouseup(function(e) {
clickeditem = $(e.target);
if (clickeditem.hasClass('checkboxinfotd')) {
prevsibling = $(clickeditem).prev()[0];
$(prevsibling).children().click();
} else if (clickeditem.hasClass('checkboxtd') || clickeditem.hasClass('checkbutton')) {
if (clickeditem.hasClass('checkboxtd')) {
clickeditem = clickeditem.children()[0];
} else if (clickeditem.hasClass('checkbutton')) {
} else {
}
console.log("clicked on checkbox");
thirdclass = clickeditem.attr('class').split(' ')[2];
if (thirdclass === "fa-square-o") {
newclass = 'fa-check-square-o';
clickeditem.removeClass("fa-square-o");
clickeditem.addClass(newclass);
} else if (thirdclass === "fa-check-square-o") {
newclass = 'fa-square-o';
clickeditem.removeClass("fa-check-square-o");
clickeditem.addClass(newclass)
}
};
});
.checkboxinfotd {
cursor: pointer;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<Table id="belowtable">
<tbody class="scenariotbody">
<TR>
<TD colspan="2">
Text
</TD>
</TR>
<TR>
<TD class="checkboxtd"><i class="checkbutton fa fa-square-o" aria-hidden="true" id="plan-282"></i>
</TD>
<TD class="checkboxinfotd">Finance department</TD>
</TR>
<TR>
<TD class="checkboxtd"><i class="checkbutton fa fa-square-o" aria-hidden="true" id="plan-289"></i>
</TD>
<TD class="checkboxinfotd">Sales department</TD>
</TR>
</tbody>
</Table>