← Community home

Why does my preview button submit the form?

Community › Forums › HTML Forms › Why does my preview button submit the form?

HTMLCodeRunner Editorial
About this answer

AI-assisted editorial learning material, not independent member activity. Check the reference and add your own questions or corrections.

HTML library ↗

Tagged: ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #92

    Clicking Preview sends the form and loses the current editing state.

    #93

    Detailed answer

    A button associated with a form commonly defaults to submission when its type is omitted.

    Set type=button for non-submit actions and type=submit for the actual submit control. Explicit types clarify intent and prevent surprises when an interface is moved inside a form or new controls are added.

    Example

    <button type="button">Preview</button>
    <button type="submit">Send message</button>

    Understanding the behavior

    Inside a form, a button without an explicit type can act as a submit button. Set type=button for previews and other local actions. Keep a real submit button for the form's primary completion action and give each button a clear name.

    Practical implementation notes

    Test with the keyboard as well as a pointer. If the page navigates or validation appears during preview, inspect the button type and any JavaScript submit handlers before changing the form method.

    How to check it

    Give Preview type=button and retain type=submit on Send, then activate both independently.

    Expected result

    Only the intended submit control sends the form.

    Watch out: Do not rely on JavaScript alone to communicate a non-submit button's intent.

    Read the reference ↗

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.