Cookbook Home ~ alanwsmith.com ~ links ~ podcast ~ mastodon

Prevent A Form From Submitting To View Its Data

Overview

Example

HTML

<form id="target-form">
  <input type="text" name="alfa" value="bravo" />
  <input type="submit" name="submit" value="submit" />
</form>

JavaScript

const stopSubmission = (event) => {
    event.preventDefault()
    const theData = new FormData(event.target)
    for (const key of theData.keys()) {
        console.log(`${key} = ${theData.get(key)}`)
    }
}

document.addEventListener('submit', stopSubmission)

Notes

References