Using Vue.js to bind labels and radio buttons in a form

My goal is to generate a dynamic list of form polls based on the data. However, using :for or v-bind:for does not result in any HTML markup appearing in the browser, causing the labels to not select the corresponding input when clicked. I have prepared a JSFiddle for reference (Note: SCSS may not work in JS Fiddle). This project is built with Nuxt.

Here is the code snippet: https://jsfiddle.net/mc4rdle/o19bgjpe/2/

Markup:

<div class="option" v-for="answer in poll.answers" :key="answer.answer.id">
  <input type="radio" :id="answer.id" :value="answer.answer">
  <label class="option" :for="answer.id">
     <div class="indicator"></div>
     <div class="label">{{ answer.answer }}</div>
   </label>
</div>

Data: polls: [

{
                    id: 1,
                    question: 'How do you feel about your current salary?',
                    answers: [
                        {
                            id: 1,
                            answer: 'Satisfied'
                        },
                        {
                            id: 2,
                            answer: 'Content'
                        },
                        {
                            id: 3,
                            answer: 'Unhappy'
                        }
                    ]
                },
                {
                    id: 2,
                    question: 'What group activity should we do??',
                    answers: [
                        {
                            id: 1,
                            answer: 'Yoga'
                        },
                        {
                            id: 2,
                            answer: 'Table Tennis'
                        },
                        {
                            id: 3,
                            answer: 'Pints'
                        }
                    ]
                }
            ]

Thank you in advance :)

Answer №1

Your input identifier is currently duplicated as it is solely based on the answer's id. To make it unique, you can combine both the poll.id and answer.id, like this: poll.id + '-' + answer.id


<div class="option" v-for="answer in poll.answers" :key="answer.answer.id">
      <input type="radio" :id="poll.id + '-' + answer.id" :value="answer.answer" :name="poll.id">
      <label class="option" :for="poll.id + '-' + answer.id">
        <div class="indicator"></div>
        <div class="label">{{ answer.answer }}</div>
      </label>
    </div>

Check out the demo on jsfiddle

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

Displaying a Vue component inside a Laravel view for seamless integration

Exploring Vue and Laravel for the first time as part of my testing journey with these frameworks. I'm experimenting with loading a Vue Component using an array. I am considering passing the data to the Laravel view via props in the Vue Component, but ...

Encountering a missing value within an array

Within my default JSON file, I have the following structure: { "_name":"__tableframe__top", "_use-attribute-sets":"common.border__top", "__prefix":"xsl" } My goal is to add values by creating an array, but I am encountering an issue where my ...

configure dynamic content within the slider element

Currently, I am experimenting with two jQuery plugins (awkward/Coda Slider 3) to implement a sliding effect for DIV content. Everything seems to be working smoothly until I attempt to set dynamic content (using JavaScript) after creating the plugin object. ...

Jade (Pug) base HTML Page: Unable to locate element: #app

My server is running on the Vibed framework and I am using the Pug preprocessor (formerly known as Jade). Below is a snippet of my page code: doctype html html head script(src="https://unpkg.com/vue") script(src="app.js") title ...

The variable "vehicles" being referenced in the rendering process is not defined within the Vue.js instance

New to Vue and php, I recently began following a tutorial on these technologies. While everything seems to be working fine, I encountered an error related to fetching vehicles. After some troubleshooting, it appears that the issue is not with the API call ...

Traverse a tree structure of nested child objects in an Angular template using a JavaScript

Check out the Javascript object below: { "id": "1554038371930_ajhnms9ft", "name": "CPS", "nodes": [ { "id": "1554038905938_le34li2cg", "name": "Consumer Journey", "nodes": [ { ...

Track user engagement across multiple platforms

Looking for solutions to log system-wide user activity in my Electron app. I want to track mouse-clicks and keystrokes to determine if the user is inactive for a certain period of time, triggering a timer reset within the application. I believe I may nee ...

Is there a way to send an array of objects to my Express / SQL Server in react?

I am currently facing a challenge with passing an array of objects to my back end (SQL Server). Below is the specific array of objects: console.info(results) Here is the array of objects named results: [0] [ [0] { [0] answer: 'bmbm,nn', [ ...

Angular's onreadystatechange event is triggered when the state

Hey there, I'm new to Angular and had a question. Is it possible to use the $http service in Angular to trigger a function whenever there is any change in the ready state/status, not just for success or failure? If so, what would be the equivalent ang ...

"Getting Started with Vue.js: Exploring the setup() and mounted

I recently started using Vue.js and am currently tackling a project. In my setup(), I've declared some constants, with one of them named "c". I found that I can still use console.log(this.c) in mounted() to print out the constant, but when I attempt t ...

Enabling a variable to encompass various types within ReactJS

In my code, I have defined two distinct types as follows: type A = $ReadOnly<{ a: ?$ReadOnlyArray<string>, b: ?string, }>; and type B = $ReadOnly<{ c: ?$ReadOnlyArray<boolean>, d: ?number, }>; Now, I am looking to create a ...

Encountering difficulties while trying to access the SQLite database file through a JavaScript Axios GET request

Having trouble opening an sqlite DB file from a js axios.get request which is resulting in an exception message being outputted to the console. The request is supposed to call my PHP controller to retrieve data from the DB and return it json-encoded. On t ...

What is the recommended lifecycle hook in Vue.js2 to execute a function when the page is loaded?

I have a dynamic table that can be filled with various numbers of rows, and I want to add an overlay before the data is loaded using my applyOverlay() function. Below is the structure of my HTML: <table id="table" class="datatable" s ...

The protection ensured by employing an extensive hash in the query string for the recognition of a changing document

I am currently developing a small web application that creates exercise program printouts. The concept is simple - a user like a personal trainer can craft an exercise regimen and send it to their client via email using a unique link. http://www.myurl ...

What is the best way to ensure that a mapped type preserves its data types when accessing a variable?

I am currently working on preserving the types of an object that has string keys and values that can fall into two possible types. Consider this simple example: type Option1 = number type Option2 = string interface Options { readonly [key: string]: Op ...

Improving the functionality of multiple range slider inputs in JavaScript codeLet me

Is it possible to have multiple range sliders on the same page? Currently, all inputs only affect the first output on the page. Check out an example here: http://codepen.io/andreruffert/pen/jEOOYN $(function() { var output = document.querySelectorAl ...

Separate the iframe sessions

I am working with 6 iframes from the same domain but with different URLs and subdirectories. Each iframe sets a cookie with the same name but a different value using the HTML header "set-cookie". To prevent interference between these cookies, I need to fin ...

Tips on incorporating the wait function within the evaluation_now action in Nightmare?

While I am incorporating Nightmare actions in my script, a question arises regarding the use of the wait function within the evaluate_now function. How can I utilize the wait function within the evaluate_now function? I am aware that I can simply use the ...

What causes the "v-col" width to suddenly alter after adding the "bg-red rounded" class?

I'm struggling to organize the data into blocks using Vuetify. When I apply the "bg-red rounded" class to the "v-col", it alters the width of the column, resulting in an undesired grid structure. Here is the template section of my Vue file. Some data ...

Issues with jQuery not detecting click events

Here is an example of HTML: <div class="sortable-buttons"> <ul> <li><a>Recent</a></li> <li><a>Popular</a></li> <li><a>Being Discussed</a></li> </ul> </div ...