Skip to content

Instantly share code, notes, and snippets.

@AndrewStanton94
Last active August 29, 2015 14:11
Show Gist options
  • Save AndrewStanton94/46ebf1e77dd614a8b254 to your computer and use it in GitHub Desktop.
Save AndrewStanton94/46ebf1e77dd614a8b254 to your computer and use it in GitHub Desktop.
Form with js submit eventListener that overrides default behaviour. Without preventDefault the form values are added to URL.
<!DOCTYPE HTML>
<html>
<head>
<title>Dynasite 2c</title>
<meta charset = 'UTF-8'>
<link rel="icon" href="favicon.ico">
<script src='ds2c.js'></script>
<style>
fieldset {border-radius: 5px}
</style>
</head>
<body>
<h1>ds2c &ndash; an active form</h1>
<fieldset>
<legend>Personal Data</legend>
<form id=myfirstform>
Name: <input type=text name=who value='Fred Bloggs'>
<br>
Gender &nbsp;
<input type=radio name=gender value=m> M &nbsp;
<input type=radio name=gender value=f> F &nbsp;
</form>
</fieldset>
</body>
</html>
window.addEventListener(
'load',
function (event)
{
var firstform = document.getElementById('myfirstform');
firstform.addEventListener(
'submit',
function (event)
{
alert('submit'); event.preventDefault();
}
);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment