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

Steps to extract date selection from a datepicker using jQuery

I recently implemented this code snippet to utilize datepicker for displaying dates: $(document).ready(function(){ $("#txtFrom").datepicker({ numberOfMonths: 1, onSelect: function (selected) { var dt = new Date(selected); ...

Knockout.JS encounters difficulty retrieving the value attribute in a select tag when the data is sourced from the server

After making an ajax request and binding the dropdown list, I noticed that the select tag was not reading the value attribute. However, when I bind the drop down list in the view model, it does recognize the value. For example: It works if I bind the mode ...

The "initialized" event in angular2-tree-component fires prior to the data being loaded

Utilizing the angular2-tree-component, my goal is to display an already expanded tree. According to Angular docs, the initialized event should be used for expanding the tree after the data has been received: This event triggers after the tree model has ...

An alternative to PHP's exec function in Node.js

I am interested in developing a piece of software using C (such as prime number factorization) and hosting it on my web server with Node.js. Following that, I would like to create an HTML document containing a form and a button. Upon clicking the button, ...

What is the significance of the A tag when parsing a URL?

So, I encountered a problem that involved parsing a URL to extract the hostname and pathname, then comparing the extracted pathname with a string. If you need help with this issue, check out this post: How do I parse a URL into hostname and path in javasc ...

Improved method for retrieving a subtask within a personalized grunt task?

As someone who is just starting out with Grunt and has only created a few custom grunt tasks, I've come up with what might be seen as an unconventional solution for traversing the initConfig to subtasks. My approach involves putting together a regex a ...

What is the most effective approach for addressing errors in both the server and client sides while utilizing nodejs and express?

Seeking the most effective approach for handling errors in a response - request scenario. Here is an example of a route that receives a request: app.get('/getInfo', function (req, res, next) { let obj = {} try { obj = { ...

Implementing the "$store" property within Vue components

Click here for a guide on how to type the $store property. Unfortunately, I've been encountering issues with it. In my Vue 2 project created using vue-cliI, I included a vuex.d.ts file in ./src directory but the $store property in my components still ...

Generating individual div elements for every piece of data in ReactJS with the help of Redux

I am utilizing redux to manage the data within a React application. Each block of data is displayed within its own DIV element. My goal is to have each div separated by space and transformed into an accordion card. I am seeking guidance on the best appro ...

How to verify changes in session variable using PHP and AJAX

Hey there! I'm looking for a way to continually monitor changes in a session variable within PHP. Let's say the session variable "x" starts off with a value of "1" and then, after five seconds, it changes to "2". This session variable "x" is up ...

Is it necessary to delay until the entire page finishes loading in Selenium 3?

public boolean CheckPageLoadStatus(){ final ExpectedCondition<Boolean> pageLoadCondition = new ExpectedCondition<Boolean>() { public Boolean apply(final WebDriver driver) { return ((JavascriptExecutor) driver).executeScr ...

Getting information from MongoDB using Node.js and Angular

Currently, I am facing difficulty in retrieving data from MongoDB (I'm also using Mongoose) and sending it to Angular in order to populate the ng-repeat list with the retrieved data. I have managed to either display the data on a blank page directly f ...

Unable to access current props within useEffect block

When I use useEffect with the parameter props.quizStep, my function fn (which is a keydown event listener) is unable to access the current value of props.quizStep. I'm puzzled as to why it's not working properly. Can you help me understand? Bel ...

Vue component updating its model only upon input element losing focus

I'm a beginner with vue and I'm currently working on incorporating an ajax search feature that triggers when a keyup event occurs. I have noticed that the model only updates when the input element loses focus. Sample HTML Code: <input name=" ...

The Magic of jQuery Cross Site Fetch

It seems like this should be simple, but I am having trouble figuring it out... I'm using jQuery to fetch a remote page from a different server, grab the HTML content, and then insert that content into a hidden DIV. However, when I try to do this wit ...

What is the best way to send variables from JavaScript to PHP while utilizing Ajax technology?

While I have noticed similar questions like this one before, I want to address my specific concerns. In previous examples, the questioner referred to using Ajax in a format similar to this: $.ajax({ type: "POST", url: 'logtime.php', ...

Utilizing JavascriptExecutor in Selenium Webdriver to start playing a video

Is it possible to trigger the play function in a page's JavaScript jQuery code using JavascriptExecutor? Below is an example of code extracted from a website: <script type="text/javascript"> jQuery(document).ready(function($) { $('#wp_mep ...

Attempting to send a GET request from localhost:8080 to localhost:3000 is proving to be a challenge as I keep encountering a CORS error. Even after trying to install CORS on my node.js server, the issue

While attempting to send an axios GET request from my front-end app on localhost:8080 to my Node.js/Express.js server on localhost:3000, I encountered a CORS error. Despite installing the cors npm package and using it as middleware in my Node.js/Express.js ...

The first time I tried using React-app, I encountered an error that read "[email protected] postinstall"

I've been struggling to set up create-react-app, encountering the same issue repeatedly. I tried uninstalling and reinstalling node.js, but only the package.json and my-app folder were created. No src or other required folders were generated. Termina ...

Generating an array of quarter numbers and year based on the current date in Node.js using Moment.js - a simple guide

I need to generate an array of quarter numbers and year numbers based on the current timestamp in Node.js. For instance, if the current quarter is Q1 and the year is 2020, I want to create an array similar to the one below. quarters = ['Q2-2019' ...