iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Input Form Attributes

A handful of attributes that normally belong on <form> can also be set on individual <input> (and <button>) elements. They override the form's setting for that one control.

Input-side overrides

AttributeEffect
formLink this control to a form elsewhere in the document by its id — no need to be nested inside it.
formactionOverride the form's action URL for this button only.
formmethodOverride the form's HTTP method.
formenctypeOverride the form's encoding.
formtargetOverride where the response opens.
formnovalidateSkip validation when submitting through this button.

Why this is useful

The classic case is a form with two submit buttons — "Save draft" and "Publish" — that should go to different URLs:

<form action="/posts" method="post">
    <textarea name="body"></textarea>
    <button type="submit">Publish</button>
    <button type="submit"
            formaction="/posts/draft"
            formnovalidate>Save draft</button>
</form>
Tip: The form attribute lets you place inputs anywhere on the page — handy when CSS layout fights against a single wrapping <form>.

Example

Example
<!DOCTYPE html>
<html>
<head>
    <title>Input Form Attributes</title>
</head>
<body>

<h1>Input Form Attributes</h1>
<p>This is a demo page for the "Input Form Attributes" lesson.</p>

</body>
</html>
Try it Yourself »

Exercise

Show a hint inside an empty input that disappears on typing.

<input type="text" ="you@example.com">

Test yourself

Q1. Override the form's method on one button with…
Q2. Override the form's action on one button with…
Q3. Associate an input to a form outside its parent with…

Discussion

Loading…