w3resource

HTML5: How to specify one or more forms the input element belongs to?

Go to Exercise page

Solution:

HTML Code:

<!DOCTYPE html><!-- Declaration specifying the document type -->
<html><!-- Opening tag for the root element of the HTML document -->
<head><!-- Opening tag for the document's header section -->
<meta charset="utf-8"><!-- Metadata tag specifying the character encoding of the document -->
<title>How to specify one or more forms the input element belongs to</title><!-- Title of the HTML document -->
</head><!-- Closing tag for the document's header section -->
<body><!-- Opening tag for the document's body section -->
<form method="POST" action="https://www.w3resource.com/html-css-exercise/basic/solution/input-form-answer-demo.php" id="form1"><!-- Form element defining a form with method, action, and id attributes -->
First name: <input type="text" name="fname"><br><!-- Text input field for entering the first name -->
<input type="submit" value="Submit"><!-- Submit button to submit the form data -->
</form><br><!-- Closing tag for the first form -->
<p>The "Address" field below is outside the form element, but still part of the form.</p><!-- Paragraph explaining the following input field -->
Address: <input type="text" name="address" form="form1"><!-- Text input field for entering the address, associated with the form with id "form1" -->
</body><!-- Closing tag for the document's body section -->
</html><!-- Closing tag for the root element of the HTML document -->

Explanation:

  • This HTML code demonstrates how to associate an input field with a specific form using the form attribute.
  • The form element with the id "form1" is defined with method and action attributes to specify the form's behavior and where the form data should be submitted.
  • Inside the form, there is a text input field for entering the first name and a submit button.
  • The "Address" input field is placed outside the form element but is still considered part of the form because it has the form attribute set to "form1", associating it with the form with id "form1".

Live Demo :

See the Pen input-form-answer by w3resource (@w3resource) on CodePen.


See the solution in the browser

Supported browser

Firefox logo Chrome logo Opera logo Safari logo Internet Explorer logo
YesYesYesYesYes

Go to Exercise page

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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/input-form-answer.php