Question:
I have two different checkbox
in the same table column, but both have different names and values.
<td align="center">
<input type="checkbox" id="chkSiapeServidor" name="chkSiapeServidor"/>
<input type="checkbox" id="chkIdUnidadeAnterior" name="chkIdUnidadeAnterior"/>
</td>
When loading the page, I check if a hidden
is filled and if so, I set the hidden
value in the checkbox. I use this code:
if($('#siapeServidor').prop('value')) {
$(this).find('td input[type=checkbox][name=chkSiapeServidor][value='+$('#siapeServidor').prop('value')+']').prop('checked', true);
}
How do I mark the checkbox
that is at the same level as the referenced one, and how to reference the td
parent tr
where they are located?
Answer:
-
to reference the next element you can use .next()
-
to find the
tr
common / closest advise .closest ( 'tr')
In your code I believe you can remove the if
condition because jQuery doesn't give an error if it doesn't find the element. And if it's an input
you can use .val()
which is the API for inputs.
$(this).find('td input[type=checkbox][name=chkSiapeServidor][value='+$('#siapeServidor').val()+']').prop('checked', true);