I want to pass <tr> id value instead for this.id , How to get tr id value. please help me thanks
<tr id="1">
<td>
<input type="checkbox" onclick="toggle(this.id)" value="private" name="private" id="private_1">
</td>
<td>
<input type="checkbox" onclick="toggle(this.id)" checked="" value="public" name="public" id="public_1">
</td>
</tr>
<br>
<tr id="2">
<td>
<input type="checkbox" onclick="toggle(this.id)" checked="" value="private" name="private" id="private_2">
</td>
<td>
<input type="checkbox" onclick="toggle(this.id)" value="public" name="public" id="public_2">
</td>
</tr>
If the structure is exactly like this, you can use
Of course this will brake as soon as the structure changes.
Instead of binding to the
clickevent, you should bind to thechangeevent. Then you also cover changes made by the keyboard.As every handler is doing the same, you should consider to leverage event bubbling. Instead of attaching the event handler to every
inputelement in your HTML, attach only one event handler to the table:This looks more complicated but has the advantage that you don’t have to write
onclick="toggle(this.id)"on everyinputelement.DEMO