HTML5: How to specify one or more forms the label belongs to?
Solution:
HTML Code:
Explanation:
- The HTML document starts 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, including the character encoding (<meta charset="utf-8">) and the title of the document (<title>).
- Inside the <body> section, there's a <p> element displaying a paragraph.
- Below the paragraph, there's a <form> element, which creates a form for user input. The method attribute is set to "post", indicating that form data will be submitted via HTTP POST method. The action attribute specifies the URL where the form data will be sent upon submission, and assigns an id "form1" to the form.
- Within the form, there are two sets of <input> and <label> elements. The first <label> element is associated with the first radio button using the "for" attribute, and the second <label> element is associated with the form using the "for" attribute and the id of the form.
- The radio buttons have names "answer" and values "yes" and "no", respectively. The radio button with the "checked" attribute is initially selected.
- Finally, an <input> element with type "submit" is used to create a submit button within the form, allowing users to submit the form data.
Live Demo:
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.