Created
July 14, 2023 10:17
-
-
Save oscarmarina/a0cc8a26d881227a7e72b2146ab8ed48 to your computer and use it in GitHub Desktop.
Requests to submit the form. Unlike submit(), this method includes interactive constraint validation and firing a submit event, either of which can cancel submission.
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
(prototype => { | |
if (typeof prototype.requestSubmit === 'function') { | |
return; | |
} | |
const validateSubmitter = (submitter, form) => { | |
if (!(submitter instanceof HTMLElement)) { | |
throw new TypeError('The submitter element is not of type HTMLElement'); | |
} | |
if (submitter.type !== 'submit') { | |
throw new TypeError('The submitter element is not a submit button'); | |
} | |
if (submitter.form !== form) { | |
throw new DOMException( | |
'The submitter element is not owned by this form element', | |
'NotFoundError', | |
); | |
} | |
}; | |
// eslint-disable-next-line no-param-reassign | |
prototype.requestSubmit = function requestSubmit(submitter) { | |
if (submitter) { | |
validateSubmitter(submitter, this); | |
submitter.click(); | |
} else { | |
const submitterNode = document.createElement('input'); | |
submitterNode.hidden = true; | |
submitterNode.type = 'submit'; | |
this.appendChild(submitterNode); | |
submitterNode.click(); | |
this.removeChild(submitterNode); | |
} | |
}; | |
})(HTMLFormElement.prototype); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment