OGI OGI - 7 months ago 21
Javascript Question

How can I set focus on nearest input field when I click on table row. Something like <label> without text

I have table with two columns and many rows. On the left side I will have once text, once nothing. On the right side I will have some input's or textarea's fields.

How can I set focus on input field when I click on left side on table row?

<label>
working fine but only on text not on TR.

Answer

Style the label to fill the cell.

td label {
  display: block;
  height: 100%;
  width: 100%
}
<table>
  <tr>
    <td>
      <label for="a">foo</label>
    </td>
    <td>
      <input id="a">
    </td>
  </tr>
  <tr>
    <td>
      <label for="b">barnj fklj kl</label>
    </td>
    <td>
      <input id="b">
    </td>
  </tr>
  <tr>
    <td>
      <label for="c">lorum ipsum</label>
    </td>
    <td>
      <input id="c">
    </td>
  </tr>
  <tr>
    <td>
      <label for="d">*fdhj*fdhj*</label>
    </td>
    <td>
      <input id="d">
    </td>
  </tr>
</table>