Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
reportValidity polyfill for IE 10, 11
if (!HTMLFormElement.prototype.requestSubmit) {
HTMLFormElement.prototype.requestSubmit = function() {
const submitBtn = document.createElement('input');
submitBtn.type = 'submit';
submitBtn.hidden = true;
this.appendChild(submitBtn);
submitBtn.click();
this.removeChild(submitBtn);
};
}
if (!HTMLFormElement.prototype.reportValidity) {
HTMLFormElement.prototype.reportValidity = function() {
if (this.checkValidity()){
return true;
}
if (this.noValidate){
this.noValidate = false;
this.requestSubmit();
this.noValidate = true;
} else {
this.requestSubmit();
}
return false;
};
}
@nuxodin

This comment has been minimized.

Copy link

nuxodin commented Apr 17, 2020

Great work!
requestSubmit has a "submitter" argument
https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/requestSubmit
This can worn work (not tested):

if (!HTMLFormElement.prototype.requestSubmit) {
    HTMLFormElement.prototype.requestSubmit = function(submitter) {
        let submitBtn = submitter;
        if (!submitBtn) {
            submitBtn = document.createElement('input');
            submitBtn.type = 'submit';
            submitBtn.hidden = true;
            this.appendChild(submitBtn);
        }
        submitBtn.click();
        !submitter && this.removeChild(submitBtn);
    };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.