Skip to content

Instantly share code, notes, and snippets.

@beardedtim
Last active June 11, 2022 21:25
Show Gist options
  • Save beardedtim/1d24db1c9e7255fc435687799d8cb685 to your computer and use it in GitHub Desktop.
Save beardedtim/1d24db1c9e7255fc435687799d8cb685 to your computer and use it in GitHub Desktop.
Basic Form in JS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Input Example</title>
</head>
<body>
<form>
<div class="input_group">
<label for="input">
Some Input
</label>
<input id="input" name="some-input" />
</div >
<button type="submit">
submit
</button>
</form>
<script>
window.addEventListener('load', () => {
const form = document.querySelector('form')
form.addEventListener('submit', e => {
const formData = new FormData(form)
const value = formData.get('some-input')
// or
const valueFromHTML = document.getElementById('input').value
})
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment