JavaScript DOM Scripting By Example Editing and Filtering Names Filter Invitees Who Have Not Responded

Can't understand what label does and why it is useful

While I was reviewing one of the videos I stumbled upon the "label" HTML tag and I couldn't understand the benefit of using the "label" tag and how it works. Thank you for your time.

Jens Höfflin
Jens Höfflin
19,934 Points

A <label> is used as a label for an <input> element in a HTML form. If a user clicks on the label it will activate the input element. A label can be bound to an element either by using the "for" attribute, or by placing the element inside the <label> element. Example usage:

<form action="some action"> <label for="male">Male</label> <input type="radio" name="gender" id="male" value="male"><br> <label for="female">Female</label> <input type="radio" name="gender" id="female" value="female"><br> <label for="other">Other</label> <input type="radio" name="gender" id="other" value="other"><br><br> <input type="submit" value="Submit"> </form>

Thank you very much.

Tom Geraghty
Tom Geraghty
24,147 Points

Just cleaning up Jens' code using Markdown:

<form action="some action">
  <label for="male">Male</label> 
  <input type="radio" name="gender" id="male" value="male"><br> 

  <label for="female">Female</label> 
  <input type="radio" name="gender" id="female" value="female"><br> 

  <label for="other">Other</label> <input type="radio" name="gender" id="other" value="other"><br><br> 
  <input type="submit" value="Submit"> 
</form>

"for" is the id of the form element that the label should be associated with

So the label for male points to the input with type radio and id male above.