Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associate label to input with class not id?

Tags:

html

forms

I know that you can associate a label with an input using the for and id attributes. However can you use a class and not an id? Thanks

<label for="rooms">Number of rooms</label> <select id="rooms">                     <option value="1">1</option>                     <option value="2">2</option>                     <option value="3">3</option>                     <option value="4">4</option> </select> 
like image 497
Evanss Avatar asked Aug 06 '12 15:08

Evanss


People also ask

How do you associate a label with an input?

There are two ways to pair a label and an input. One is by wrapping the input in a label (implicit), and the other is by adding a for attribute to the label and an id to the input (explicit). Think of an implicit label as hugging an input, and an explicit label as standing next to an input and holding its hand.

Can we give class to label tag?

You can give it a class, yes.

Can we use ID in label tag?

The <label> tag can be used in two ways:The <label> tag needs a for attribute whose value is the same as input id. Alternatively, <input> tag use directly inside the <label> tag. In this case, the for and id attributes are not needed because the association is implicit.

Can HTML label have ID?

An id on a <label> tag assigns an identifier to the label. The identifier must be unique across the page.


1 Answers

Classes are not unique (you can have multiple elements with the same class), so no.

If you want to associate a label to an input without using ID, you can implicitly assign it by including said input inside of the label:

<label>Number of rooms     <select name="rooms">         <option value="1">1</option>         <option value="2">2</option>         <option value="3">3</option>         <option value="4">4</option>     </select> </label> 
like image 99
Madara's Ghost Avatar answered Sep 21 '22 04:09

Madara's Ghost