The input value linked with Vue is not displaying on the DOM

I am attempting to connect a property of an item within an array in the data object of Vue to the value attribute of an input tag using binding. However, when I inspect the DOM of a web browser, it does not display.

<template v-for="role in roles">
            <tr>
                <td>{{ role.Name }}</td>
                <td>
                    <button type="button" class="btn-danger" @@click="RemoveRole(role)">Remove</button>
                </td>
                <td hidden>
                    <input type="text" :id="role.Id" name="role[]" v-model="role.Id"/> // results in just "<input type="text" id="..." name="role[]" />"
                    // there is no value attribute visible, but the Id attribute can give the right value?
                </td>
            </tr>
        </template>

Although the id attribute can provide the correct value, why doesn't the value attribute work?
:value="role.Id" does not have any effect.

Any insights on this issue would be appreciated.

Answer №1

It's perfectly normal to see this behavior. Vue handles the value attribute of an <input> in a special way by setting it as a property rather than an attribute. Essentially, the compiled template utilizes:

input.value = role.Id

instead of:

input.setAttribute('value', role.Id)

You can refer to the relevant Vue source code at the following link:

https://github.com/vuejs/vue/blob/98b4d683f578bb09c4e56f35048e49441c590a41/src/compiler/parser/index.js#L837

The implementation of platformMustUseProp can be found here:

https://github.com/vuejs/vue/blob/98b4d683f578bb09c4e56f35048e49441c590a41/src/platforms/web/util/attrs.js#L11

If you remove the hidden attribute from the surrounding <td>, you should observe that the <input> contains the correct value. It may not be necessary for the <input> to have a value attribute, as most functionalities access the value using input.value.

In scenarios where the <input> is intended to be hidden, consider using type="hidden" instead of type="text".

Answer №2

<input type="text" :id="position.Id" name="position[]" :value="position.Id"/>

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

The updates made to a form selection using Ajax do not reflect in jQuery's .serialize or .val functions

When using the .load jQuery function to retrieve a form and place it in the document body, I encounter an issue. After loading the form, I manually change the select value and attempt to save the form using an ajax .post request. However, when trying to ...

Why are the $refs always empty or undefined within Vue components?

I am completely new to working with Vue and I've been attempting to utilize $refs to retrieve some elements within the DOM from a sibling component. My objective is very basic, as I just need to obtain their heights, but I haven't had much succes ...

Using Axios to Integrate an API - Displaying a Username and Password Pop-up

I have been given a project that involves API integration. I have successfully retrieved data from the API, but there is a pop-up appearing asking for a username and password. Although I have these credentials, I would like to log in without that pop-up ap ...

Converting an unbroken series of string values into organized key-value pairs for easy reference

I assure you this is not a duplicated question. Despite my attempts with JSON.parse(), it seems to be ineffective. Here's the issue at hand: I recently received assistance from an answer that was both crucial and enlightening. However, the code prov ...

Is there a way for me to identify when I am "traveling" along the same path?

I want to create a toggle effect where a view is hidden if the user tries to revisit it. This can be useful for showing/hiding modal boxes. Here's the code I attempted: /* Root Instance */ const app = new Vue({ router, watch: { '$route&a ...

ReactJS Modal popping up repeatedly while inside a loop

Hey there, I've been experimenting with ReactJS and came across this amazing Modal Component for opening videos in a modal. However, I encountered an issue when placing the Modal inside a loop with multiple links - it opens multiple times correspondin ...

What is the best way to link one element to another element?

Apologies for the mismatched title, but here's my issue: I need to create hidden checkboxes for each Polymer's paper-button div in jsp/style related problems. <%int i; %> <form ACTION="Computation.jsp"> <% for(i=0; i<n; ...

Unable to store the data retrieved from MySQL into an array

Every time I try to add the object result1 to the array a, it ends up empty. Can someone help me figure out what I'm doing wrong? app.get('/freezer', (req, res) => { var a = []; var sql = `SELECT freezer_id FROM freezer_data`; ...

When data is submitted through the POST method, it mysteriously disappears

When I send the "user" variable to another page called Survey.php, here's how it looks: In the index.html file: <form action="Survey.php" method="post" name="frm"> <div><input type="text" name= ...

When an action is dispatched, Vuex modifies two elements within the state

I am encountering an issue with my Vuex store in a Vue2 and Vuex2 setup. Upon dispatching a specific action, it alters the state of 2 elements in my store. Here's a snippet of the state with some dummy data for clarity: { "clients":[ ...

The issue arises when trying to escape double quotes within a regex pattern using ng-pattern

My ng-pattern validation includes a regex pattern ^[^\./:*\?\"<>\|]{1}[^\/:*\?\"<>\|]{0,254}$ to prevent invalid characters in file paths and set a limit. However, when I specify the ng-pattern as: ng-p ...

Updating or swapping images using JavaScript and HTML

I am looking to implement a feature similar to Twitter, where uploading a picture automatically updates the avatar by displaying a spinner while the new image loads. I attempted to accomplish this with the following code: <script language="javascript"& ...

Using ng-click to manipulate variables within a list

In my ionic/angular/phonegap app, I have a list of items and I'm attempting to use an action sheet to pass in the variables. However, I am having trouble accessing the variable on the same line as the ng-repeater. Here is the markup: <ion-view vi ...

vue.js throwing error: Unable to access 'getters' property of an undefined object

Within the Form component.vue, there is a substitution of the event object from the getter into the v-model: <template> <form @submit.prevent="submitForm"> <div class="form-group row"> <div c ...

JavaScript: Accessing the selectedIndex of a dropdown list in RadGrid

Currently facing an issue with utilizing the RadGrid control from Telerik. I am attempting to access and manipulate the value of a GridDropDowncolumn within RadGrid using JavaScript (while in edit mode). Does anyone have any suggestions for the correct Ja ...

Ways to determine if a form has been submitted without a submit button

Currently, I am utilizing an ajax request to submit a form through a link. However, I am facing a challenge in receiving inputs in my php code. Typically, I use the following conditional statement: if($_SERVER["REQUEST_METHOD"]=="POST&&isset($_REQ ...

Sorting tables in Jquery with advanced filter options and seamless integration with an ajax pager

I've implemented a tablesorter library for sorting and filtering data in a table along with a plugin that allows me to paginate the table across multiple pages. Due to the increasing number of records in my table causing slow loading times (>60 se ...

Running a repeated script

I recently discovered a fantastic script called Typer that creates a typewriter effect. However, I noticed that it does not have a built-in method to loop the animation once it reaches the end. Does anyone have any suggestions on how to achieve this? t ...

AngularJS routing with html5mode causing 404 error when using htaccess

I am currently working on my very first angularjs application using version 1.6x, and I am encountering some 404 errors with my router. Here is how my router is set up: app.config(function($routeProvider, $locationProvider) { $locationProvider.html5M ...

Feathers.js - Incorporating Static Content

Recently, I've been exploring the capabilities of feathers.js for a new project and have been quite impressed with its potential. Intrigued by what it offers, I decided to embark on creating a basic content management system as a way to further my lea ...