15

I want to add a <input> element, more specifically a checkbox, in my table. The following works:

<tbody key={rule._id}>
  <tr>
    <td>{rule.deviceId}</td>
    {
      <input
        name="isEnabled"
        type="checkbox"
        checked={rule.enabled}
      />
    }
    <td>{rule.name}</td>
  </tr>
</tbody>

but it produces an error in the console: <input> cannot appear as a child of <tr>

Is there a 'proper' way to do this?

Mayank Shukla
  • 92,299
  • 16
  • 152
  • 142
grpcMe
  • 1,067
  • 4
  • 12
  • 27

4 Answers4

32

Put the <input> inside a <td>.

Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264
3

You missed one <td> pair. Also the { and } are not required. Should be like that

<tbody key={rule._id}>
  <tr>
    <td>{rule.deviceId}</td>
    <td>
      <input
        name="isEnabled"
        type="checkbox"
        checked={rule.enabled} />
    </td>
    <td>{rule.name}</td>
  </tr>
</tbody>
Fotis
  • 1,260
  • 15
  • 30
1

tr can only contain td. You should wrap your input with a td.

<tbody key={rule._id}>
  <tr>
    <td>{rule.deviceId}</td>
    <td> 
      <input
        name="isEnabled"
        type="checkbox"
        checked={rule.enabled}
      />
    </td>
    <td>{rule.name}</td>
  </tr>
</tbody>
Joseph Combs
  • 812
  • 9
  • 12
An Nguyen
  • 1,467
  • 10
  • 20
0

You must put it in a <td> Element, and you can add styles to the <td> DOM.

JiangangXiong
  • 2,126
  • 1
  • 10
  • 14