Skip to content

Instantly share code, notes, and snippets.

@nuxodin
Created April 8, 2020 21:32
Show Gist options
  • Save nuxodin/3ae174f2a6a112df3ccad22459237a91 to your computer and use it in GitHub Desktop.
Save nuxodin/3ae174f2a6a112df3ccad22459237a91 to your computer and use it in GitHub Desktop.
Event.submitter polyfill
!function(){
var lastBtn = null
document.addEventListener('click',function(e){
if (!e.target.closest) return;
lastBtn = e.target.closest('button, input[type=submit]');
}, true);
document.addEventListener('submit',function(e){
if (e.submitter) return;
var canditates = [document.activeElement, lastBtn];
for (var i=0; i < canditates.length; i++) {
var candidate = canditates[i];
if (!candidate) continue;
if (!candidate.form) continue;
if (!candidate.matches('button, input[type=button], input[type=image]')) continue;
e.submitter = candidate;
return;
}
e.submitter = e.target.querySelector('button, input[type=button], input[type=image]')
}, true);
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment