Issue with Vue v-model not updating values of an array when v-for is used

I am a beginner with Vue and I'm exploring the possibilities of v-model. My goal is to create an array of editable names. Below is the code I have been working on:

var app = new Vue({
  el: '#app',
  data: {
    names: ['Josh', 'Tom']
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.3/vue.min.js"></script>

<div id="app">
  Editable list:
  <ul>
    <li v-for="name in names"><input type="text" v-model="name"></li>
  </ul>
  {{ names }}
</div>

Although the inputs display correctly and show the expected values, updating them does not reflect changes in the names array as it should. What could be causing this issue?

Answer №1

Instead of using v-model to manipulate a value directly, you should be operating on a vue data field. Here's an example:

var app = new Vue({
  el: '#app',
  data: {
    people: [{name: 'Josh'}, {name: 'Tom'}]
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.3/vue.min.js"></script>

<div id="app>
  Editable list:
  <ul>
    <li v-for="person in people"><input type="text" v-model="person.name"></li>
  </ul>
  {{ people }}
</div>

Alternatively, you can also use the following approach:

var app = new Vue({
  el: '#app',
  data: {
    names: ['Josh', 'Tom']
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.3/vue.min.js"></script>

<div id="app">
  Editable list:
  <ul>
    <li v-for="(name, index) in names"><input type="text" v-model="names[index]"></li>
  </ul>
  {{ names }}
</div>

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

What is the reason behind JavaScript subtracting the timezone offset for ISO dates when passed into the Date() function?

In my function, I handle different valid date strings to produce a JavaScript Date object. Most strings work as expected, however, when an ISO formatted date (YYYY/MM/DD) is provided, the user's timezone offset is deducted from the date. For example ...

Is it possible for a Vue.js build to encounter errors due to unregistered components?

Exploring a component template... <template> <Unknown></Unknown> </template> In the context of this template, Unknown could be either a globally registered component or not. Upon encountering this scenario at runtime, an informa ...

Automatically populate the form with data from a MongoDB database in Express, based on the user's previous selection

I am currently in the process of developing an online hospital appointment booking website using express and mongodb. I need to dynamically display available time slots from the database based on the user-selected date without any redirection. I attempted ...

What is the process to convert an IEnumerable of Anonymous Type in C# into a JavaScript Object?

Within my ASP.net C# code, I have an IEnumerable container that contains objects of an anonymous type (loosely based on SQL data). If we take a look at a snippet of my code: var uics = entities.getData() .Select(x => new { id = ...

Updating route from action within Vuex Store

Exploring ways to trigger a route change from the store. I attempted to import router directly into the store and use the following code: LOG_OUT({commit}){ commit('LOG_OUT__MUTATION'); router.push({ name: 'Login' }) } Unfo ...

Is there a way for me to view the properties within the subcomponents?

Working on a project to create a bulletin board using React, following the official documentation. Decided to consolidate all actions related to the bulletin board into one alert component called AlertC. In the Form onSubmit statement, if the title is tr ...

Tips for determining whether a user is part of an array of users with a mongoose query

Currently, I am exploring ways to determine if a student is enrolled in a course using mongoose. Below are the schemas I am working with: Course schema: const mongoose = require("mongoose"); const User = require("../models/User&quo ...

Using console.log() within a method while chaining in JavaScript/jQuery

I've been experimenting with developing jQuery plugins and I'm interested in chaining methods. The jQuery tutorial (found here: https://learn.jquery.com/plugins/basic-plugin-creation/) mentions that you can chain methods by adding return this; at ...

Jquery validation is ineffective when it fails to validate

I have implemented real-time jQuery validation for names. It functions correctly in real-time, however, when I attempt to submit the form, it still submits the value even after displaying an error message. Below is the code snippet for the validation: $ ...

Ban on the utilization of emojireact-role in external channels

How can I ensure that the bell reaction only gives a role in a specific channel? The provided code is below. Additionally, is there a way to restrict this feature so only administrators can assign roles through reacting with a bell emoji? Any assistance ...

Issue encountered while attempting to include a background image in React JS

I encountered an issue when attempting to include a background image in react js. ./src/index.css (./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-4-1!./node_modules/postcss-loader/src??postcss!./src/index.css) Unable to locate module: An attem ...

What is the format required for specifying the end date and start date when using

Implementing the bootstrap datepicker. The Documentation doesn't specify the expected format for setEndDate or setStartDate. It refers to endDate option, which also lacks details on the required format. I want to set endDate and newDate based on a s ...

Tap on the div nested within another div

Here is the scenario I am dealing with: <div id="main"> <div id="a"></div> <div id="b"></div> </div> On my website, element B is positioned below element A. How can I attach a click event only to those A elements t ...

The redirection did not occur as no authorization token was detected

I have two Nodejs applications, one for the front-end and the other for the back-end. The back-end app is secured with token access using express-jwt and jsonwebtoken middlewares. My issue is as follows: when I make a request from the front-end to the bac ...

function name cannot be invoked correctly in JavaScript

I am facing an issue with calling my javascript function correctly when I click on it (show_more_in_month_0/1/2). Below is my current code: $i = 0; foreach ($data as $row){ echo '<td><span class="glyphicon-plus show_more_in_mont_'. ...

Am I on the right track with my service definition in Angular?

(function(){ angular.module('myApp',[]) })(); (function(){ angular.module('myApp.dashboard',[]) })(); (function(){ angular.module('myApp.value',[]) })(); (function(){ 'use strict'; angular.modu ...

The React page loads before verifying the clients' permissions

In my application, I am using a Javascript query to fetch the data of the current user. This data is then used to verify the user's access permissions for different pages in my React app with the help of Apollo Client and GraphQL. Unfortunately, ther ...

Is there a way to create a multi-page website simulation using jQuery?

My current project involves creating a single page website, but I am looking to give the illusion of a multi-page site by using CSS/jQuery to hide and reveal different sections when specific links in the navigation menu are clicked. Here is the code snipp ...

List item with React Material UI tooltip

click here for image preview Exploring the idea of incorporating a customized tooltip component using React Material UI. The goal is to leverage the tooltip, list item, and list components provided by React Material UI. I have experimented with utilizing ...

Selenium and JavaScript integration for opening new browser tabs

Hello there, I am looking to open new tests ('it' method) in new tabs and currently trying this approach: driver = new Builder().forBrowser('chrome').build(); beforeEach(() => { // driver.manage().window().open('url') ...