Last active
December 19, 2015 20:08
-
-
Save ryanburnette/6010916 to your computer and use it in GitHub Desktop.
A better pattern for binding jQuery events to DOM elements. This avoids event mangling.
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
var $events = $('body') | |
, submitButton = '.login form .submit' | |
; | |
function saveForm(ev) { | |
var $form = $(this) | |
, data | |
; | |
ev.preventDefault(); | |
ev.stopPropagation(); | |
if (!ajaxy) { | |
$form.submit(); | |
} else { | |
data = $form.serializeArray(); | |
$.ajax({ body: data }); | |
} | |
} | |
$events.on('click', submitButton, saveForm); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment