HTML5: How to specify a label for an option-group?
Solution:
HTML Code:
<!DOCTYPE html><!-- Declares the document type and version of HTML -->
<html><!-- Begins the HTML document -->
<head><!-- Contains metadata about the document -->
<meta charset="utf-8"><!-- Specifies the character encoding of the document -->
<title>How to specify a label for an option-group</title><!-- Sets the title of the document -->
</head><!-- Ends the head section -->
<body><!-- Contains the content of the document -->
<form name="Fav_Sport"><!-- Defines a form with the name "Fav_Sport" -->
<label>Favorite Sports</label><!-- Provides a label for the form -->
<select name="favoritesports" id="favoritesports"><!-- Defines a drop-down list with a name and ID -->
<optgroup label="Sports"><!-- Starts a group of related options labeled "Sports" -->
<option label="Soccer">Soccer</option><!-- Defines an option for soccer within the "Sports" group -->
<option label="Hockey">Hockey</option><!-- Defines an option for hockey within the "Sports" group -->
<option label="Tennis">Tennis</option><!-- Defines an option for tennis within the "Sports" group -->
<option label="Golf">Golf</option><!-- Defines an option for golf within the "Sports" group -->
</optgroup><!-- Ends the group of sports -->
</select><!-- Ends the drop-down list -->
</form><!-- Ends the form -->
</body><!-- Ends the body section -->
</html><!-- Ends the HTML document -->
Explanation:
- The HTML document begins with a declaration of its type and version (<!DOCTYPE html>).
- The <html> element encloses the entire HTML document.
- The <head> section contains metadata about the document, such as character encoding and title.
- The <meta charset="utf-8"> tag specifies the character encoding of the document as UTF-8.
- The <title> tag sets the title of the document.
- Inside the <body> section, there is a <form> element with the name "Fav_Sport".
- The <label> element provides a label "Favorite Sports" for the form.
- The <select> element defines a drop-down list with the name "favoritesports" and the ID "favoritesports".
- Inside the <select> element, there is an <optgroup> element, which starts a group of related options labeled "Sports".
- Inside the <optgroup>, there are <option> elements, each representing a different sport with its label attribute defining the display text.
- The HTML document is properly closed with the </html> tag.
Live Demo:
See the Pen optgroup-label-answer by w3resource (@w3resource) on CodePen.
See the solution in the browser
Supported browser
Yes | Yes | Yes | Yes | Yes |
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/html-css-exercise/basic/optgroup-label-answer.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics