How to use similar label-for in different form elements, as "id" is unique in HTML standard?

337 views Asked by At

In HTML standards;

  • 2 same name attributes can be used in different form elements in the same html document.
  • id attribute has to be uniqure in the same html document.
  • label for requires id to work

So, the following is valid

<html>
  <body>
    <form id="form1">
      <input name="abc">
    </form>
    <form id="form2">
      <input name="abc">
    </form>
  </body>
</html>

However, the following is invalid if I use label for** because id is not unique**

<html>
  <body>
    <form id="form1">
      <label for="item">Click me</label>
      <input name="item" id="item">
    </form>
    <form id="form2">
      <label for="item">Click me</label>
      <input name="item" id="item">
    </form>
  </body>
</html>

So, do I use label for incorrectly or is this a bug related to label for in standards?

1

There are 1 answers

0
bro On

You are only supposed to have a unique id for every selector on a webpage. Why? For starters, the id field is so you can hyperlink to the exact section of a page. you can have multiple classes but only 1 id with a particular name.

I think what you are looking for is value="" http://www.w3schools.com/html/html_form_input_types.asp

http://www.w3schools.com/css/css_selectors.asp