When using Vue, here's a trick to verify the presence of a value in a form even when the value attribute is

I am currently in the process of creating multiple forms and I need to verify if all input fields are filled out or not. Since I am utilizing data binding with the model, I am not using the value attribute. This means that when attempting to iterate through all fields, it will show empty values.

Is there a solution to this issue?

// template segment
<form v-for="questions in data">
    <div v-for="question in questions">
        <input type="text" v-model="forms[question.id]"/>
    </div>
</form>

// JavaScript code 
document.querySelectorAll('form').forEach(form => {
    form.querySelectorAll("input").forEach(input => {
      console.log(input.value);// this will display as empty

      // Additional logic needs to be implemented here to notify user if any inputs are left empty
    })
})

Answer №1

It's worth noting that in Vue, using "querySelector" is not typically recommended. Vue operates as a reactive framework, meaning that it automatically handles rendering whenever there are changes to the reactive data.

Rather than directly manipulating the DOM, focus on examining the forms array:

const hasEmptyInput = forms.some(answer => !!answer);

if (hasEmptyInput) {
  // Handle error or display message
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error message: Expected function in JavaScript runtime when using JQuery and Ajax

I'm relatively new to this and could not find a solution for my issue online. On my asp.net webpage, I have the following script that is causing an error stating that a function is expected. Interestingly, when I use hardcoded values for the city na ...

Understanding the functionality of a notification system

Planning to create an admin tasking system utilizing PHP, MySQL, and JavaScript. Curious about how the notification system operates and how it stores real-time data. Are there any good examples of a notification system? ...

Display intricate header and preview in a printed datatable

Hey there, I've been using the Datatable plugin and it's really great. However, I've come across a problem with complex headers like this: <thead> <tr><td>some text</td></tr> <tr><td>some te ...

Leveraging Javascript/jquery to retrieve image data for a specific frame within a video

Query My aim is to optimize the code for image manipulation on a web browser using JavaScript. One possible solution I am considering is utilizing a video file to reduce HTTP requests. Is there a way to extract a single frame from a video in png, jpg, or ...

Create static HTML files using an Express server

Recently, I developed a nodejs web project using express-js and ejs. However, upon further reflection, it occurred to me that hosting it as static html files on Netlify might be more cost-effective than running it as a nodejs app on Heroku. Since the data ...

What is the process for extracting the value of a checkbox generated through JavaScript?

I recently came across a helpful post on Stack Overflow that provided sample code demonstrating how to display multiple list of checkboxes dynamically on a dropdown list. The function in the code was exactly what I needed for my webpage. However, I encount ...

How can I retrieve a large API response in Nuxt without causing the page to render slowly?

I have a dynamic page in my Nuxt project where I utilize Fetch and Axios to retrieve all the necessary data, such as photos and text. The issue I'm facing is that the API response is quite long, with more than 3800 lines of code. When loading the pa ...

Sort by the date using a specific data attribute

My main objective is to showcase a multitude of posts on this website, sourced from a server that does not organize them by date. I, however, wish to arrange them based on their dates. Currently, the layout on my website looks something like this: < ...

enhancing the style of my Express project with CSS and EJS

Currently, I am facing challenges with linking CSS to my EJS files in a project I am developing using Node/Express/EJS. Despite creating a public folder with a CSS subfolder and the main.css file inside, I am unable to get the CSS to display in the browser ...

What is causing the initial activation of the <button> within a form?

Within my <form>, I have included 2 submit buttons. The first button looks like this: <button class="regular" id="geocodesubmit" style="height:40px;">Set Location</button> The second button looks like this: <button type="submit" ...

What is the best way to choose an unidentified HTML element that contains text content?

In today's world, we often use CSS modules or other methods to hide classes and IDs. However, there are times when we need to select elements using JS selectors, and that can be a bit tricky. Let's take an example. If we look at the home screen ...

Having trouble deleting multiple rows with ng-repeat in datatables

Having followed the instructions in this post, I quickly integrated it with jquery datatables. However, the functionality is not as expected. When attempting to delete rows, they do not get deleted. Furthermore, if I navigate to the next page and return, ...

The resolution of deferred actions is not as successful as foreseen

In my code, I have a method that queries documentdb for a specific document and returns the results to the caller. fetch: function(query) { var fetchDeferred = q.defer(); client.queryDocuments(collectionLink, query).toArray(function(err, docs) { ...

Interactions between a service and a directive: interdependence or triggering of events

Issue: managing multiple directives that interact with a service or factory to communicate and log actions with a server. Should I establish dependencies between them? angular.module('someModule', []) .directive('someDir', ['l ...

The data returned by the Axios response.data object is full of nonsensical information when making a

I tried experimenting with Axios by writing a quick test function, but the response.data object I received was all jumbled up. I've been searching online for a solution, but I haven't been able to find one. It seems like all the Axios tutorials o ...

The Vue.js error message "Unable to access property 'array_name' as it is undefined" indicates an issue with

I'm currently working on fetching data using Axios requests and storing it in an array. Below is the code I have been using: props: [ 'products', ], data: function () { return { algolia: '', pro ...

Enhance the appearance of the Title Attribute in HTML Textareas

Could someone please assist me with styling the title attribute for an html textarea? I attempted to use this jfiddle link, but it doesn't seem to work correctly. I am working in the Chrome browser. Below is the code snippet: <li> <labe ...

Discord between Bootstrap tabs and C3 charts: A Compatibility Str

On my website, I have implemented Bootstrap navigation tabs that each contain a chart. The issue I am facing is that when I navigate to the home page, the chart in the active tab displays perfectly fine. However, for the other tabs, the charts overlap with ...

Is it better to throw an exception before doing any filtering?

Checking for the existence of removeId in the items before filtering to avoid exceptions. Is there a more efficient way to handle this without using two loops? This code may not be optimized due to the double looping process. const items = ...

The setInterval function continues executing even after the page has been changed

I'm encountering an issue with my function where it continues to run even after the page has changed, resulting in an error. How can I go about stopping this behavior? Thank you! componentDidMount() { var current = 0; var slides = document.g ...