Skip to content
Tutorials Center
My learning

Topics

Lesson 68 of 122 ยท Forms

Text Areas and Buttons

Collect longer text with textarea (rows, cols, maxlength, wrap) and master buttons, including submit, reset and button types, button vs input, and formaction.

A single-line text box is perfect for a name or an email address, but not for a message, a review or a shipping note. For that you need <textarea>. And every form ends with at least one button, which sounds trivial until a button submits a form you did not mean to submit.

This lesson covers both: how to collect multi-line text, and how to choose and configure the right kind of button.

The <textarea> element

A <textarea> is a multi-line text box. Unlike <input>, it is not a void element: it has an opening and a closing tag, and the text between them is its starting value:

HTML
<label for="message">Your message</label>
<textarea id="message" name="message" rows="5" cols="40">Hello!</textarea>

There is no value attribute on a textarea. To start empty, put the closing tag right after the opening tag, with nothing in between, not even a line break:

HTML
<textarea id="notes" name="notes"></textarea>

Whitespace inside a textarea is kept exactly as written. If you indent the closing tag on a new line, those spaces and the line break become part of the starting value, and people find a mysterious blank line in the box.

One line break is forgiven. The HTML parser drops a single line break directly after the opening <textarea> tag, so you can start the content on the next line. Everything else, including indentation before the closing tag, is kept.

Sizing with rows and cols

  • rows sets the visible height in lines of text (default 2).
  • cols sets the visible width in average characters (default 20).

These are only the starting size. In practice most sites set the width with CSS (width: 100%) and keep rows for a sensible height. Most browsers also let people drag the corner to resize the box. The CSS resize property controls that: resize: vertical is a popular choice, as it lets people make the box taller without breaking your layout.

Boxes that grow as you type. The newer CSS declaration field-sizing: content lets a textarea grow with its content. It is supported in all current major browsers (Baseline newly available since June 2026); in older browsers the box simply keeps its normal size, so it is safe to add.

Limiting length

maxlength caps the number of characters a person can type, and minlength sets a minimum that is checked when the form is submitted. Pair a limit with a visible counter so people are not surprised when typing suddenly stops:

A textarea with a character counter

Press Enter a few times while typing. The line breaks are part of the submitted value; on the server they arrive as a carriage return plus line feed (\r\n), whatever operating system the person uses.

The wrap attribute

Long lines wrap visually inside the box. The wrap attribute decides whether those visual wraps are also sent:

  • wrap="soft" (the default) sends the text as typed. Visual wrapping is only for display.
  • wrap="hard" inserts real line breaks where the text wrapped, so the submitted value matches what was on screen. It only works together with cols, which determines where wrapping happens.

You will rarely need hard. It exists for older systems that expect fixed-width lines, such as some plain-text email workflows.

The <button> element

A <button> contains its label as content, which can include text, icons and other inline markup:

HTML
<button type="submit">
  <svg aria-hidden="true" width="16" height="16"><!-- icon --></svg>
  Send message
</button>

The type attribute decides what the button does:

type What it does
submit Submits the form it belongs to. This is the default.
reset Restores every control in the form to its starting value.
button Nothing by default. Use it for buttons driven by JavaScript.

The default-type trap

Because submit is the default, a <button> inside a form submits that form unless you say otherwise. A "Show password" or "Add another row" button written as plain <button> will submit the form every time it is clicked. The fix is simple: always write type="button" on buttons that are not meant to submit.

Button types

Change the nickname and press Reset: it goes back to "Sunny". Then delete type="button" from the Show button and click it again: now it submits the form. That is the trap in action.

About the reset button: it is easy to hit by accident, and it wipes out everything someone typed with no undo. Most forms are better off without one.

<button> or <input type="submit">?

Both make a working submit button:

HTML
<button type="submit">Place order</button>
<input type="submit" value="Place order">

The difference is content. <input> is a void element whose label comes from its value, so it can only show plain text. <button> can contain markup, such as an icon next to the text, and is easier to style. Prefer <button> in new code. You will still meet <input type="submit"> in older projects, and it is perfectly valid.

Which button was pressed?

A form can have several submit buttons. Give them a name and a value, and only the button that was actually used is included in the submitted data. That is how one form can offer "Save draft" and "Publish":

HTML
<button type="submit" name="action" value="draft">Save draft</button>
<button type="submit" name="action" value="publish">Publish</button>

This is also why our demo script passes event.submitter to FormData: it includes the pressed button, just as a real submission would.

Per-button overrides: formaction and friends

Sometimes two buttons should send the form to different places, or in different ways. Submit buttons accept attributes that override the form's own settings, for that button only:

Button attribute Overrides the form's
formaction action (where the data goes)
formmethod method (get or post)
formenctype enctype (how the data is encoded)
formtarget target (where the response opens)
formnovalidate validation (skips it when present)

The form-level versions of these attributes are covered in Form Attributes. Here is how the overrides change things:

Several submit buttons

With an empty title, Publish refuses to submit, but Save draft goes through because of formnovalidate. That is a common pattern: let people save unfinished work, but only publish complete work. The Preview button sends the same data with GET to a different URL in a new tab.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you set the starting text of a <textarea>?

    A textarea's content is its starting value. It has no value attribute, and a placeholder is only a hint that is never submitted.
  2. 2. A "Show password" <button> without a type sits inside a form. What happens when it is clicked?

    The default type of a button is submit. Add type=button to buttons that should not submit.
  3. 3. Why might you prefer <button> over <input type="submit">?

    Both submit forms and both are valid. A button's label is its content, so it can hold more than plain text.
  4. 4. Which attribute lets a single submit button skip the form's validation?

    formnovalidate is the per-button override. novalidate goes on the <form> and turns validation off for every button.

Summary

  • <textarea> collects multi-line text; its starting value is its content, and whitespace is kept exactly.
  • rows and cols set the starting size; minlength and maxlength limit length; wrap="hard" sends visual line breaks.
  • A <button> is type="submit" by default; write type="button" for script buttons, and avoid reset buttons.
  • Prefer <button> to <input type="submit"> because it can contain markup.
  • Named submit buttons tell the server which one was pressed.
  • formaction, formmethod, formenctype, formtarget and formnovalidate override the form's settings for one button.

Next up: Input Attributes, the attributes that fine-tune every input.