Problem:
I have a HTML form which has a series of yes/no questions for the user to complete. I have an existing script to check if the user selects no for all the answers and shows a dialog box alert.
I now need to change this slightly so instead of checking if they selected ‘No’ for every input I now need to check if they have selected ‘No’ for any input.
Here’s my current script that checks for all No answers:
var isAllNo = Object.values(inputs).every(function(q){
return q === 'No'
});
if(isAllNo){
if (confirm('You have selected "No" for your answers. Do you wish to continue?')) {
console.log('Thing was saved to the database.');
} else {
console.log('Thing was not saved to the database.');
ev.preventDefault();
}
}
I haven’t been able to find the syntax or how to change this to check for any No responses instead.
Solution:
Replace .every
to .some
.
var isAllNo = Object.values(inputs).some(function(q){
return q === 'No'
});
if(isAllNo){
if (confirm('You have selected "No" for your answers. Do you wish to continue?')) {
console.log('Thing was saved to the database.');
} else {
console.log('Thing was not saved to the database.');
ev.preventDefault();
}
}