How to check every value meets condition in Javascript function?


I’m building a Vuejs & Laravel app and have a list of data called "questions" which I am trying to iterate through and see if the answer value within the question is not equal to null. If even a single question’s answer value is null, I want to prevent the form from submitting and use an alert to tell the user to fill in all questions.

The issue I’m having is even though all the answers are not yet filled out, I still get a ‘success’ logged in the console from my function.

Can anyone point out where I am going wrong with this?

This is my list of data:

enter image description here

As you can see, some of the answer fields are null, whereas if they are filled out there is data there.

And this is my submit function which I have bound to my form submit button:

methods: {
        submitPressed(e){
            console.log(this.questions);
            this.questions.forEach(question => {
                question.questions.every(q => {
                    if(q.answer !== null){
                        console.log('success');
                        // this.submit();
                    } else {
                        e.preventDefault();
                        console.log('more to fill out');
                    }
                })
            })}
                
        
    },

Thanks in advance!

Source: Vue – Stack Overflow

November 22, 2021
Category : News
Tags: forms | javascript | laravel | loops | Vue.js

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.