Why I need to click the button twice before it gets working? Function checks invalid input fields and passes sweetalert message when button is clicked.
document.addEventListener('invalid', (function() {
return function(e) {
e.preventDefault();
$('#submit').on("click", function() {
let valid = true;
$('[required]').each(function() {
if ($(this).is(':invalid') || !$(this).val()) valid = false;
})
if (!valid) swal({
title: "Good job",
text: "You clicked the button!",
type: "success"
}, );
})
};
})(), true);