Step-by-step guide on how to showcase elements within v-for by clicking on them

In my data array, only the first element is visible by default.

Clicking on either the YES or NO button will display the element with the corresponding id of yes_section or no_section (depending on which button was clicked).

For instance, if we click on the "no 4" button in the Example 1 block, then the Example 4 block will be displayed below. On the other hand, clicking on "yes 2" will reveal the Example 2 block.

Thank you in advance! =)

new Vue({
  el: "#app",
  data:{
    stepsData: [
        {
        id: "1",
        yes_section: "2",
        no_section: "4",
        name: "Example 1"
      },
      {
        id: "2",
        yes_section: "5",
        no_section: "3",
        name: "Example 2"
      },
      {
        id: "3",
        yes_section: "2",
        no_section: "4",
        name: "Example 3"
      },
      {
        id: "4",
        yes_section: "2",
        no_section: "4",
        name: "Example 4"
      },
      {
        id: "5",
        yes_section: "2",
        no_section: "4",
        name: "Example 5"
      },
    ]
  }
});
.step {
  background: #ccc;
  padding: 20px;
  margin-bottom: 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div 
  v-for="(step, id) in stepsData" 
  :key="step.id"
  class="step"
>
  <div class="legal-aid__step-question" v-html="step.name"></div>
    <button>YES {{ step.yes_section }}</button>
    <button>NO {{ step.no_section }}</button>
  </div>
</div>

Answer №1

To properly display the necessary step information, it is recommended to save the step_id and utilize computed properties. More details on computed properties can be found at this link.

new Vue({
  el: "#app",
  data: () => ({
    active_step_id: "1",
    stepsData: [
        {
        id: "1",
        yes_section: "2",
        no_section: "4",
        name: "Example 1"
      },
      {
        id: "2",
        yes_section: "5",
        no_section: "3",
        name: "Example 2"
      },
      {
        id: "3",
        yes_section: "2",
        no_section: "4",
        name: "Example 3"
      },
      {
        id: "4",
        yes_section: "2",
        no_section: "4",
        name: "Example 4"
      },
      {
        id: "5",
        yes_section: "2",
        no_section: "4",
        name: "Example 5"
      },
    ]
  }),
  
  computed: {
    active_step() {
      return this.stepsData.find(step => step.id === this.active_step_id)
    }
  }

});
.step {
  background: #ccc;
  padding: 20px;
  margin-bottom: 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div 
  class="step"
>
  <div class="legal-aid__step-question" v-html="active_step.name"></div>
    <button @click.prevent="active_step_id = active_step.yes_section">YES {{ active_step.yes_section }}</button>
    <button @click.prevent="active_step_id = active_step.no_section">NO {{ active_step.no_section }}</button>
  </div>
</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

Press a single button to toggle between displaying and hiding the table

$(document).ready(function() { $("#t1").hide(); // hide table by default $('#sp1').on('click', function() { $("#t1").show(); }); $('#close').on('click', function() { $("#t1").hide(); }); }); <li ...

Populate an HTML table using a JavaScript array containing objects

Greetings, fellow coders! I am new to the coding world and this community, and despite my efforts in searching for a solution, I couldn't find exactly what I was looking for. So, here is my question: I have an array structured as follows: const arr ...

Is there a way to retrieve the data instead of receiving an undefined result when making an asynchronous call?

subject in user.subjects = subjects below is undefined instead of being an array of subjects. Context: I am working with three database tables - users, subjects, and a relationship table between users and subjects known as users_subjects. The objective i ...

Update the second dropdown list based on the selection made in the first dropdown list in Vue.js

My goal is to dynamically change the options in the second select list based on the value selected in the first one. I initially achieved this by creating two separate Vue instances for each select, but I wanted a more streamlined approach for a cleaner ap ...

What is the best way to access a Node/Express API key from the .env file in front-end JavaScript code?

Currently, I am utilizing an OpenWeatherMap API key within my client-side JavaScript for a basic weather application built using Node and Express. However, I understand that this approach is not secure for production environments, so I have installed doten ...

Are there any similar tools to Graphstream in Java that can be used with HTML5 using canvas and JavaScript?

GraphStream is a revolutionary graph library created in Java that offers Java developers a simple way to visually represent dynamic graphs either in memory, on screen, or in files. Check out this demo video. With GraphStream, you can effectively handle th ...

Encountering a problem when making a HTTPS GET request to a REST API using

I am currently running an Angular application that utilizes an external API to fetch country ISOs. However, I am encountering an error with the API since it uses HTTPS. Interestingly, when I implement a proxy in my Angular local environment by mapping /is ...

When using VueJs to loop through a list of options in a dropdown menu, the value of the selected option is showing as undefined instead of

Consider the code snippet below, designed to display a form stepper. In this example, I have simplified it to show only one step: <form-wizard :formdata="this.form_data"> <form-tab stepindex="1" title="Title of Tab" :selected="true"> ...

Obtain JSON data response in PHP with the help of jQuery

Below is the code I am using to make an AJAX call and retrieve data from another PHP file: $.post('<?php echo get_site_url(); ?>/ajax-script/',{pickup:pickup,dropoff:dropoff,km:km}, function(data){ $('#fare'). ...

Determining the size of an image prior to uploading it in a React

The solution for this issue can be found using vanilla JavaScript here To clarify, instead of using alert(), my goal is to store the dimensions in the state object. How can I adapt this code into a React component utilizing the OnChange() event handler? ...

Converting a buffer to a string in Python 3, similar to Node.js 6.0.0

I am currently in the process of translating an old node.js library into Python and I am facing difficulties trying to replicate the behavior of Buffer.toString() in Python. The library is used in a node 6.0.0 environment. While researching, I came acros ...

Issue with incremental static generation in version 13 not functioning as expected

Is ISR working for anyone in the NextJS 13 Beta version? I have set revalidate to 15 as follows. export const revalidate = 15; However, even after running `npm run build`, the page still remains a statically generated site (SSG). The symbol is showing u ...

Leverage the power of forkJoin in JavaScript by utilizing objects or sourcesObject

I'm currently facing an issue with my code snippet below: getInformations().subscribe( informations => { let subs = []; for (const information of informations) { subs.push(getOtherDetails(information.id)); } ...

How can I determine the package version that is being used when requiring it in Node.js?

I am currently working on resolving an issue with a node module that does not have a package.json. The module contains references to cheerio and superagent: var log = console.log.bind(console), superagent = require('superagent'), cheerio ...

The result of the $.post() request is back

In my current code, I am using a jQuery $.post function like this: $.post('/test', json_data, function(data) { result = data }); This function is being used in a validation process where the return value (data) contains either true or false ...

Is there a method to consistently implement a controller function across all routes within a router file in Express?

I have the following code snippets within an application. I am looking for a way to apply "authController.isLoggedIn" to each instance of "router.get" without having to repeat it multiple times. Initially, I thought using router.use might be the solution ...

Issue with displaying info window when clicking on marker in react-google-maps

Seeking assistance in opening the info window of specific markers when clicked. All markers and map are displaying correctly, with correct titles appearing on hover. However, clicking a marker does not trigger the info window to show. The console confirms ...

Ways to arrange an array in JavaScript or jQuery when each array record contains multiple objects

Before giving a negative vote, please note that I have thoroughly searched for solutions to this problem and found none similar to what I am facing. I am looking to alphabetically sort the array by image.name using JavaScript or jQuery: var myArray = [{ ...

JavaScript function not being executed after AJAX response in HTML

There seems to be a problem with my jQuery code. After receiving an html response from an ajax call and prepending it to a div, a div within that received html is not triggering a function in my external javascript file. In the index.php file, I have incl ...

Is it possible to send variables within an ajax request?

I'm currently in the process of building my own website, and I have a specific requirement where I need to display different queries based on which button is clicked. Can this be achieved using the following code? Here's the HTML snippet: <d ...