How to handle multiple radio inputs and determine the checked value in Vue?

Currently, I am in the process of learning Vue.js and developing a basic poll generator. However, I have encountered an issue with radio inputs.

In this application, each question can be of two types - 'select' or 'range.' 'Select' represents a single-choice question while 'range' allows users to choose from 1-5 values using radio inputs (as illustrated in the image below; note that I will need to update the naming later on...).

The problem arises when rendering the poll as there are multiple radio input groups corresponding to each question, and selecting a value does not mark the checkbox as checked.

I retrieve the poll data along with questions and possible answers through axios.get method.

Here is a snippet of the template:

 // Please see above code 

Data:

data() {
  return {
    submission: {
      email: undefined,
      age: undefined,
      answers: []
    },
    poll: {},
    loading: false
  }
}

updateAnswer:

 // Please refer to the existing code above 

TL;DR: How can I ensure that clicked radio inputs appear as "checked"?

Although I am able to capture the data for posting to the server, the issue lies in the visual aspect where the selected option is not visibly marked for the user.

Upon my research, it seems like setting a v-model for each input may resolve the issue. However, I am unsure about how to implement this in my current scenario.

Visual Representation of Radio Inputs

Answer №1

To designate the selected :radio, you can utilize :checked.

The easiest approach is to simply include a checked property for each answer and bind it to the radio:

<input type="radio" ... v-bind:checked="answer.checked">

In essence, implement this for both types of questions (radio and range).

Adjust your method to mark an item as checked upon selection:

updateAnswer(question, answer) {
  question.QuestionAnswers.forEach(a => Vue.set(a, 'checked', false));
  answer.checked = true; // no need to call Vue.set because it is now reactive

This method clears all answers for that question and then selects the clicked question.

View the complete demo below:

/* JavaScript code goes here */
<script src="https://unpkg.com/vue"></script>

<div id="app">
  /* HTML content goes here */
</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

Testing Vue components within a Rails application is a crucial step to ensuring

I have been working on creating Vue components using a different structure. Vue.component('my-component', { template: '<div>A totally unique component!</div>' }) Now, I am looking to test this with karma and jasmine or j ...

The input box fails to show larger values on the user's page

Show me the biggest number that the user enters in an input box and then display it back to them. I need some improvements to my code, ideally making it possible with just one input box instead of two. <!DOCTYPE html> <html la ...

The functionality to generate forms on the click of a button appears to be malfunctioning

I am currently attempting to dynamically create Bootstrap Panels using an `onclick` function. However, I want each Panel to generate multiple forms when the button is clicked. In my current code, the first Panel successfully creates forms when the button i ...

Struggling to update the position of an array and dynamically show its contents using Angular

I am working with an Array of Objects, each containing an Array of strings. Utilizing ng-repeat to display all the strings, I have a button to switch to the next set of strings by changing the selected object in the outermost array. Below is the code snipp ...

What is the method to retrieve the key of a component in Vue 3's composition setup() API?

When using the traditional API, obtaining the key is straightforward: created() { console.log(this._.vnode.key); } However, when working with the composition API in setup(), the 'this' keyword is undefined. After attempting to review the dat ...

Manipulating Arrays in JavaScript: Techniques for Extracting Values Buried in Nested Objects

I am working with an array of objects that contain multiple "Book" objects with dynamic keys. My goal is to filter the objects in the array to only include those that have at least one new "Book" object. For example: const arr = [ { id: '123&ap ...

Unable to render data in Chart JS using PHP JSON

Hello, I’m currently working on creating a dynamic line chart using Chartjs. The data is being pulled from an SQL database using PHP in JSON format. Although the data is successfully retrieved, the chart appears blank. Any assistance would be greatly app ...

Tips for changing styles in a Vue Vuetify app based on language selection

Currently, I am working on a Vue.js application using Vuetify and Vue-i18n for internationalization. I have a specific font-family that I want to only be applied when the language is set to Arabic. In order to achieve this, I will need to switch styles bas ...

Typeahead in Angular is failing to function properly after using the $compile method

I have made some adjustments to the popover directive in order to include files and $compile them. While I've managed to make ng-repeats function properly, I'm facing issues when trying to add a typeahead feature. angular.module("app").directive ...

Numerous events have been successfully integrated into the angularjs-bootstrap-calendar

Currently, I am facing a challenge with loading all my event data from the server using angular-bootstrap-calendar. Due to the large volume of events, it is taking a considerable amount of time to load. I am exploring the possibility of fetching only a mo ...

How to specifically exclude a checkbox from the "select all" function in J

One way to select all checkboxes with HTML code: Select All <input type="checkbox" name='select_all' id='select_all' value='1'/> To achieve this with Javascript code: <script type="text/javascript> $(&apos ...

Why am I encountering a type error in NodeJS when utilizing the ping module?

I'm currently working on creating a simple app for pinging an IP address. The HTML form I've created has one input field for the IP address, which is then sent to NodeJS for processing. I am utilizing the ping module to retrieve the results. Ever ...

Is the definition of uniforms present in the THREE.js RawShaderMaterial?

The suggested reading regarding THREE.Js RawShaderMaterial states: Default uniforms and attributes are not automatically included in the GLSL shader code. Nevertheless, I have successfully executed the following shader using a rawShaderMaterial: ...

Tips on downloading an image using the URL in nestjs

I'm trying to retrieve a link and save the associated image in the static folder, but I seem to be encountering some issues with my code. Here's what I have so far: @Injectable() export class FilesService { createFileFromUrl(url: string) { t ...

The code is nearly identical except for one issue that causes an infinite loop within useEffect

Hey, I'm new to reactjs and I've encountered a puzzling situation with the following two code snippets. I can't figure out why using the "First code" results in an infinite loop (endless '123' log outs in my browser console) while ...

Load full html content, including doctype, into a jQuery container | Block scripts from running in iFrames

I am attempting to fetch an entire HTML file using AJAX and then manipulate the DOM with jQuery. This means that the retrieved HTML document includes a doctype and other top-level elements. The process of retrieving the HTML is straightforward: $.get(&ap ...

To what extent can the Vuetify data tables be customized?

https://i.sstatic.net/x4qhA.png I am currently working on replicating the layout shown in the image above. The table is already functional in my Vue project. The following code snippet represents the Vuetify datatable template in use: <v-card> ...

Unique creation: Bespoke Bootstrap-Vue selection box module

Struggling to create dynamic form components, particularly with checkboxes <template v-if="type === 'switch'"> <b-form-checkbox switch size="lg" :name=" ...

Is it possible to locate and eliminate the apostrophe along with the preceding letter?

My objective is to tidy up a character string by removing elements that are not essential for the user and SEO, specifically the (letter before the apostrophes) in this case. I am looking for a regex solution or explanation of how to achieve this in PHP, a ...

Configuring Jest unit testing with Quasar-Framework version 0.15

Previously, my Jest tests were functioning properly with Quasar version 0.14. Currently, some simple tests and all snapshot-tests are passing but I am encountering issues with certain tests, resulting in the following errors: console.error node_modules/vu ...