25

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
  • 100,735
  • 18
  • 158
  • 142
grpcMe
  • 1,367
  • 5
  • 15
  • 28

4 Answers4

42

Put the <input> inside a <td>.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
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,322
  • 16
  • 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
  • 909
  • 9
  • 12
An Nguyen
  • 1,487
  • 10
  • 21
0

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

JiangangXiong
  • 2,326
  • 1
  • 12
  • 14