I have been trying to solve this issue since more than 6 hours without any further progress. I'm trying modify the Bootstraps validate script. And I wanted to enclose it inside a function to return a boolean.
Simply I want 1st console.log to function before 2nd console.log so that bsvalidate will return correct value, but its always other way. I cant let the function execution hold until forEach function concludes.
I've tried to create a promise inside the main function without any success. Thanks in advance for all the help.
function bsvalidate() {
'use strict'
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.querySelectorAll('.needs-validation')
// Loop over them and prevent submission
window.check = true
Array.prototype.slice.call(forms)
.forEach(function (form) {
form.addEventListener('submit', function (event) {
if (!form.checkValidity()) {
event.preventDefault()
event.stopPropagation()
window.check = false
console.log('inside forEach') // 1st console.log
}
console.log('check>>', window.check)
form.classList.add('was-validated')
}, false)
})
console.log('outside forEach') // 2nd console.log
return window.check
}```