<form method="post" id="formFinish2">
<input type="checkbox" name="finish" />
<input type="hidden" name="id" class="finishId" value="2" />
</form>
<script type="text/javascript">
$(":checkbox[name='finish']").click(function() {
id = $(this).closest('input.finishId').val();
alert(id);
$("#formFinish".id).submit();
});
</script>
Alerts me undefined. I would like it to alert me 2, which is coming from the input that has the class finishId, next to the checkbox you clicked on.
It’s because
closestonly searches DOM upward. In other words,closeststarts searching by father element, then grandfather element, etc.You should use
siblingsinstead.