Setting a radio button dynamically based on JSON data by using a select dropdown option

I am looking to generate questions from a pre-selected list using Vue.js.

I have successfully implemented this with a radio button that reveals a new set of questions once checked.

Now, I want to achieve the same functionality using a dropdown selection. When a value is chosen from the dropdown, it should display a set of radio button questions.

Below is the code snippet for my current test:

<div id="app">
 <select v-model="selectedL">
   <option v-for="l in list" v-bind:value="{ id: l.id, text: l.name }">{{ l.name }}</option>
 </select>
 <div v-for="r in list.text" :key="r.id">
  <input type="radio" :value="r" v-model="selectedR" :id="r.id" :name="r.id">
  <label class="label" v-bind:for="r.id">{{r.name}}</label>
 </div>
</div>

var app = new Vue({
el: '#app',
data: {
selectedL: '',
selectedR: '',

list: [
    {
  id: 1,
  name: 'A',
  text:[
    {
      text1: '123',
      text2: '456'
    }
   ]
  },
  {
  id: 2, 
  name: 'B',
  text:[
    {
      text1: '678',
      text2: '908'
    }
   ]
  },
  {
  id: 3, 
  name: 'C',
  text:[
    {
      text1: '143',
      text2: '786'
    }
    ]
  }
]

}
})

The above code represents my progress with Radio buttons.

https://jsfiddle.net/bernlt/pvndg8jf/11/

I require assistance in achieving the same functionality by using a Select-option to determine the Radio questions.

Answer №1

Here is a possible solution:

var app = new Vue({
el: '#app',
data: {
  selectedL: '',
  selectedR: '',
  list: [{
    id: 1,
    name: 'A',
    text:[{
        id: 123,
        question: '123',
      },
      {
        id: 456,
        question: '456',
      }]
    },
    {
    id: 2, 
    name: 'B',
    text:[{
        id: 678,
        question: '678',
      },
      {
        id: 908,
        question: '908',
      }]
    },
    {
    id: 3, 
    name: 'C',
    text:[{
        id: 143,
        question: '143',
      },
      {
        id: 786,
        question: '786',
      }]
    }]
}
})
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<div id="app">
 <select v-model="selectedL">
   <option v-for="l in list" v-bind:value="l">{{ l.name }}
   </option>
 </select>
 
 <p>Questions:</p>
 <div v-for="(r, index) in selectedL.text" :key="r.id">
  <input type="radio" :value="r" v-model="selectedR" :id="r.id" :name="r.id">
  <label class="label" v-bind:for="r.id">{{r.question}}</label>
 </div>
 
  <p>selected Id from select: {{selectedL.id}} </p>
  <p> selected Id from radio: {{selectedR.id}} </p>
</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

Combining two JSON strings within SQL Server

I am working with a dimension table that includes a JSON column and applying SCD Type 1 to update its values. string1={ "Name":"Suneel","Age":23,} String2={"Name":"Suneel Kumar","Age":23,"Cit ...

Incorporating navigation controls into a modal window

I have successfully created a sign-in modal, but now I'm looking to include buttons at the top. One button should redirect users to register/sign up, and the other button should lead them back to the sign-in modal, as shown in the image I've link ...

Seeking guidance on Ajax .... in need of clarification

Could someone provide an explanation for this code snippet: const greeting = 'hi'; $.ajax({ type: "POST", url: "check.php", data: "greeting="+greeting, success: function(response){ alert( "Response received from server: " + resp ...

When there is only one value, the BehaviorSubject can be hit multiple times

Recently, I implemented BehaviourSubject in a shared service to retrieve the current value when clicking a button. Everything seems to be working fine, however, there are instances where the API call within the subscribe block of BehaviourSubject is being ...

Using jQuery to scroll within a table

I am currently working on a project that involves displaying specific data rows. To achieve this, I have decided to use a table. For a better understanding, you can check out a table demo here. The issue I am facing is that whenever I change the selected ...

How can we identify if a React component is stateless/functional?

Two types of components exist in my React project: functional/stateless and those inherited from React.Component: const Component1 = () => (<span>Hello</span>) class Component2 extends React.Component { render() { return (<span> ...

Problem with the getJSON function

Here is a question that has been bothering me: I am currently working on a barcode scanner script which retrieves data from a JSON file. The script itself functions properly, except for one issue. After the while loop, I want to display an error alert m ...

The display of 'App' seems to be experiencing issues within the render method

Can someone help me with this issue? I am encountering the following error: Uncaught Error: Invariant Violation: Element type is invalid - expected a string for built-in components or a class/function for composite components, but received an object. Bel ...

Using JavaScript to transform base64 encoded strings into images

I'm currently working on an app using Titanium and I have a base64 string that I need to convert into an image from JSON data. Any assistance you can provide would be much appreciated. Thank you! ...

Transferring JSON data to a non-RESTful WCF service

I am looking to implement a JavaScript function that can send an object as JSON to a WCF service for saving. Here is the current JavaScript code: <script> $(document).ready(function(){ $("#submitButton").click(function() ...

Adding JSON data to an array for Flot Diagram

I've been struggling with the task of organizing data into an array. The existing temp array is structured as follows: tmp[0][0] = "NY" tmp[0][1] = "52" tmp[1][0] = "FL" tmp[1][1] = "25" My goal is to transfer this data into a new array called data. ...

encountering issue alerts when using the MUI TextField module alongside the select function

Encountering an error in the MUI console: children must be provided when using the TextField component with select. <TextField select id="outlined-basic" label="User Name" name="user" size="small" {...teamForm.getFieldProps("user")} erro ...

Issue: Node.js Express unable to access static files when the route is nested more than one folder level deep.Resolution

Before we delve into the question, let me share with you the folder structure of my node.js express app: /home server.js index.html /app /routes public.js /public /css main.css In my server.js file, ...

Is it possible to delete a section of the URL within an anchor tag prior to the #anchor

My webpage contains a link that looks like this: <li class="jump"><a href="http://example.com/#about">About Me</a></li> I am interested in using jQuery to eliminate the 'http://example.com/' section of any URL found with ...

Fade out when the anchor is clicked and fade in the href link

Is it possible to create fade transitions between two HTML documents? I have multiple HTML pages, but for the sake of example, let's use index.html and jobs.html. index.html, jobs.html Both pages have a menu with anchor buttons. What I am aiming to ...

Snippet for VS Code: Vue-i18n

How can I efficiently incorporate an i18n string into a .vue file? ...

What is the correct way to utilize Global Variables in programming?

Having trouble incrementing the current page in my pagination script to call the next page via AJAX... In my TypeScript file, I declare a global variable like this; declare var getCurrentPage: number; Later in the same file, I set the value for getCurren ...

What is the best way to input a dynamic post array in PHP?

Explaining this might be challenging, but I will do my best. I have multiple input posts with custom conditions, which means there can be as many custom conditions as needed. Here is the HTML: <input name="type[]" value="type 1"> <input nam ...

Display a PHP array with my customized formatting

Hello, I'm struggling with a php code that is supposed to retrieve data from a table and display it in a structured format. My goal is to have the output look something like this: id|name|id|name|id|name. However, I've tried using the implode() m ...

Transforming a NSNumber into a Double value representing CLLocationDegrees

Struggling with incorporating latitude and longitude into my map annotations using JSON data. "{\"lat\": 25.0437396, \"lng\": 121.5308224}" First, I convert it into a dictionary format ["lat": 25.0437396, "lng": 121.5308224] and ut ...