Last active
January 4, 2016 19:49
-
-
Save danielboendergaard/8669906 to your computer and use it in GitHub Desktop.
Formaction/formtarget polyfill
This file contains hidden or 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 _input = document.createElement('input'); | |
if ( ! ('formTarget' in _input && 'formAction' in _input)) { | |
$('[type="submit"]').on('click', function (e) { | |
// Prevent default form submit | |
e.preventDefault(); | |
// Find parent form and store values | |
var $this = $(this), | |
$form = $this.closest('form'), | |
formTarget = $form.attr('target'), | |
formAction = $form.attr('action'); | |
// Check if a target is defined and set the form target accordingly | |
if ($this.attr('formtarget') !== null) | |
$form.attr('target', $this.attr('formtarget')); | |
// Check if an action is defined and set the form action accordingly | |
if ($this.attr('formaction') !== null) | |
$form.attr('action', $this.attr('formaction')); | |
// Submit form | |
$form.submit(); | |
// Set form back to initial values | |
$form.attr('target', formTarget); | |
$form.attr('action', formAction); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment