This repository has been archived by the owner on Apr 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
My first version wasn't usable. I've rewritten the code to be able to make use of the function.
- Loading branch information
1 parent
5b8f3c6
commit ffb9281
Showing
4 changed files
with
65 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,31 @@ | ||
"use strict"; | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.preventSubmission = preventSubmission; | ||
function preventSubmission(submission, callback) { | ||
submission.preventDefault(); | ||
if (callback) { | ||
callback(); | ||
exports.default = preventFormSubmission; | ||
|
||
|
||
/** | ||
* Sets up a form with an event listener on the submit event that prevents | ||
* submission. | ||
*/ | ||
function preventFormSubmission(formId, callback) { | ||
var form = document.getElementById(formId); | ||
|
||
if (form === null) { | ||
throw new Error('The form with id ' + formId + ' was not found on the page.'); | ||
} else { | ||
return; | ||
var submitHandler = function submitHandler(submission) { | ||
submission.preventDefault(); | ||
|
||
if (callback) { | ||
callback(); | ||
} else { | ||
return; | ||
} | ||
}; | ||
|
||
form.addEventListener('submit', submitHandler, { capture: true, passive: false, once: false }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,25 @@ | ||
// @flow | ||
|
||
export function preventSubmission(submission: Event, callback?: () => void): void { | ||
submission.preventDefault(); | ||
if (callback) { | ||
callback(); | ||
/** | ||
* Sets up a form with an event listener on the submit event that prevents | ||
* submission. | ||
*/ | ||
export default function preventFormSubmission(formId: string, callback?: () => void): void { | ||
const form = document.getElementById(formId); | ||
|
||
if (form === null) { | ||
throw new Error(`The form with id ${formId} was not found on the page.`); | ||
} else { | ||
return; | ||
const submitHandler = function(submission: Event) { | ||
submission.preventDefault(); | ||
|
||
if (callback) { | ||
callback(); | ||
} else { | ||
return; | ||
} | ||
} | ||
|
||
form.addEventListener('submit', submitHandler, {capture: true, passive: false, once: false }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,25 @@ | ||
// @flow | ||
|
||
export function preventSubmission(submission: Event, callback?: () => void): void { | ||
submission.preventDefault(); | ||
if (callback) { | ||
callback(); | ||
/** | ||
* Sets up a form with an event listener on the submit event that prevents | ||
* submission. | ||
*/ | ||
export default function preventFormSubmission(formId: string, callback?: () => void): void { | ||
const form = document.getElementById(formId); | ||
|
||
if (form === null) { | ||
throw new Error(`The form with id ${formId} was not found on the page.`); | ||
} else { | ||
return; | ||
const submitHandler = function(submission: Event) { | ||
submission.preventDefault(); | ||
|
||
if (callback) { | ||
callback(); | ||
} else { | ||
return; | ||
} | ||
} | ||
|
||
form.addEventListener('submit', submitHandler, {capture: true, passive: false, once: false }); | ||
} | ||
} |