-11 Vote

HTML: Label for Checkbox in other Cell of a Table

Question by Guest | 2014-04-27 at 18:23

I have included some checkboxes on my website which are arranged using a table. The arrangement is not the problem, but I am using the label-tag and unfortunately, it is not working with the table.

When using labels outside of tables or within a table cell it is working without any problems:

<label><input type="checkbox" name="cb" value="1"> Text</label>

But as soon as trying to arrange the checkbox in another table cell than the text of the label, it is not working anymore:

<tr>
  <td><label><input type="checkbox" name="cb" value="1"></td>
  <td>Text</label></td>
</tr>

So, the first example is working, the second example is not working. What can I do? For my arrangement it would be nice to divide the checkbox from the label text, but it is not working like this.

ReplyPositiveNegative
2Best Answer4 Votes

In your case, you should use the "label for" attribute.

Here is an example:

<tr>
  <td>
    <input type="checkbox" id="cb" name="cb" value="1">
  </td>
  <td>
    <label for="cb">Text</label>
  </td>
</tr>

As you can see, in this case the label-tag is only around the text. With the attribute "for", you can tell this label the ID of the checkbox, it corresponds to.

So, it is possible to separate label and checkbox like you want.
2014-04-27 at 21:55

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.