The syntax appears to be correct to me, but only the "if" statement functions. I have even tried with just a simple if/else statement and only the "if" statement functions that way as well. What am I doing wrong? I have seen similar questions on SO as well, but they did not answer my question because when I tried to use their corrections in my case, it still didn't work.
HTML
<table>
<tr>
<td><input type="checkbox" value="test" /></td>
<td><a class="clickme" href="#">edit</a></td>
</tr>
</table>
JS
$('.clickme').click(function () {
if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked', false)) {
$(this).closest('td').prev().find('input[type=checkbox]').prop('checked', true);
console.log('Test2');
} else if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked', true)) {
$(this).closest('td').prev().find('input[type=checkbox]').prop('checked', false);
console.log('Test');
}
});
$('.clickme').click(function () {
if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked', false)) {
$(this).closest('td').prev().find('input[type=checkbox]').prop('checked', true);
console.log('Test2');} else if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked', true)) {
$(this).closest('td').prev().find('input[type=checkbox]').prop('checked', false);
console.log('Test');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td><input type="checkbox" value="test" /></td>
<td><a class="clickme" href="#">edit</a></td>
</tr>
</table>