A small quiz lacks interactivity

I'm currently working on creating a small quiz, but I'm facing some challenges in getting it to function properly.

My goal is to use Vue Class Binding to highlight the question that has been answered incorrectly when the 'submit' button is clicked. However, it seems like the Class Binding is not reacting as expected when the form is submitted. It only seems to work correctly after I make some changes to the values.

I have recorded a video demonstration showing the issue:

Below is the relevant piece of code:

<template>
  <form @submit.prevent="handleSubmit">
    <fieldset v-for="(item, questionIndex) in questions" :key="questionIndex">
      <legend :class="{ wrong: isFalse[questionIndex] }">{{ item.question }}</legend>
    </fieldset>
    <button type="submit">Submit</button>
  </form>
</template>

<script>
    export default {
        asyncData(context) {
            return context.app.$storyapi.get('cdn/stories', {
                version: 'draft',
                startsWith: '/subjects/biology/hl/1999-may'
            }).then(response => {
                console.log(response.data.stories[0].content.question);
                return {
                    questions: response.data.stories[0].content.question
                }
            });
        },
        data() {
            return {
                choices: [],
                isFalse: []
            }
        },
        methods: {
            handleSubmit() {
                for (let i in this.choices) {
                    if (this.choices[i] === this.questions[i].answer) {
                        this.isFalse[i] = false;
                    } else {
                        this.isFalse[i] = true;
                    }
                }
            }
        }
    }
</script>


<style>
.wrong {
        color: red;
    }
</style>

Answer №1

Understanding the concept of array reactivity is crucial. Make sure to review the caveats section in the documentation

To resolve this issue, consider using Vue.set/vm.set

this.$set(this.isFalse, i, false)

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

React JS error: Trying to use props.video as a function, but it's not

As a newcomer to React, I'm encountering some errors that I need help debugging. Error #1: Uncaught TypeError: props.videos.map is not a function at new VideoList Error #2: bundle.js:19956 Error: findComponentRoot(..., .0.0.0): Unable to find el ...

The functionality of this code is optimal in Chrome and Firefox, yet it encounters issues in IE

Currently, I am attempting to create a jquery tooltip that will pop up when a user hovers over a link. The link in question is set to display:block style in order to cover a larger area. Surprisingly, this setup works flawlessly in Chrome and Firefox, but ...

The Close button on the Facebox Jquery box mistakenly closes the master page instead of the popup

I am currently encountering an issue with the Facebox jquery box. I have a form inside it that contains a Submit Button and a Cancel Button. The problem arises when I click on the Submit Button - instead of closing the Facebox window, it closes the Main p ...

Using the PUT method in Node.js to set the ID

Need help with setting ID value from frontend apiRoutes.put('/intake', function(req, res) { Intake.findById({id, function(err, intake) { if (err) res.send(err); check : true; intake.save(function(err) { ...

In JavaScript, the array is being passed as "undefined"

<html> <head> <script> function replaceLocation() { var locationArray= new Array(); locationArray[1,2,3,4]=document.getElementById("From").value.split(" "); document.getElementById("map").src="https://maps.google.com/maps?f=d& ...

I can't seem to get my border-bottom to show up in my Vuetify

I'm currently working on a website project in nuxt.js with Vuetify. The issue I am facing is adding a white line under the app bar to separate it from the links I plan to add as extensions. Despite trying v-divider, I couldn't get rid of the spac ...

Building with Webpack on a started Express server can be achieved by following these steps

Hello there, I am diving into the world of React and Webpack. My configuration in `webpack.config.js` looks like this: const webpack = require('webpack'); const path = require('path'); module.exports = { cache: true, entry: { ...

What steps do I need to take to convert a view from a three.js camera and scene to a bcf cameraview?

I am attempting to convert a ifc.js viewer file into a bcf format. The ifc.js viewer utilizes a three.js webglrenderer along with a camera and scene setup. Shown below is an example: https://i.sstatic.net/oTphJ.png https://i.sstatic.net/8Dtag.png I would ...

Creating an HTML button that triggers multiple functions

Hi there! I'm currently working on a web application that requires a play and pause button for audio playback. So far, I've successfully created individual buttons for playing and pausing the audio. However, I now need a single button that toggle ...

Is there a way to verify if a user is currently in a voice channel using DiscordJS V13?

I am attempting to verify if the user who initiated a command is currently in a voice channel. After running my bot, I receive the correct voice channel information. However, if I leave the channel and try the command again, I still get back the same cache ...

The split function of a string displays an undefined result

My goal is to extract all characters that come after the equal sign within a URL: let url = this.$route.query.item console.log(typeof(url)) // outputs string let status = url => url.split('=')[1] When I run the code, it shows &apo ...

Discover the potential location of an element using jQuery's animate feature

Struggling to obtain the correct position of an element due to the length of my animation causing $(this).position().top to be calculated prematurely. Is there a way to predict the future position value of an element before it moves to that specific locat ...

Determine the number of items within a variable using the jQuery selector for class '.item'

Is there a way to determine the number of elements with the class ‘item’ that are stored in a variable? Please refer to this link var post = '<div/><div class="item"></div><div class="item"></div><div class="item ...

Attempting to generate a jwt results in an undefined value

Currently in the process of mastering Node.js and I am in the midst of transitioning my existing API from Python to Node. My main challenge lies in the creation of a jwt token for authentication purposes with a third-party API. However, it seems I'm e ...

Using Angular to Dynamically Change Webview URLs

I've been attempting to update the URL of my webview by invoking a function with AngularJs, but unfortunately, I haven't been successful. Below is my Angular class: app.controller("speechController", function($scope){ $scope.page = 'in ...

What is the interaction between Parse Cloud Code and Parse Server like?

After reviewing Parse's documentation on Cloud Code, I find myself puzzled. They make it clear that Cloud Code is not running in a Node.js environment. What does this mean for the functionality of my server? Even though the server uses Node.js & Exp ...

Guide on invoking the server-side method in Office 365 using JavaScript files

Exploring the capabilities of office 365 API apps and looking for documentation on how to access specific row(s) and cell(s) values / select a particular sheet programmatically in a .js file. Currently, I am utilizing the following code within a function: ...

How to style a date and time object using angularjs

What is the best way to convert a PHP datetime object to the format "May-27-1990"? ...

Is there a method to manually generate a cookie for Internet Explorer using InnoSetup?

Is there a way to manually create a cookie in InnoSetup on behalf of a specific website, such as www.stackoverflow.com, similar to how JavaScript cookies are stored? Javascript cookie: function setCookie(cname,cvalue,exdays) { var d = new Date(); d.s ...

The issue I am facing is that the MDCSelect:change event does not seem to be functioning properly

Is there a way to get MDCSelect:change to work properly before appending all options? If I put MDCSelect:change after the list append, it works but the UI doesn't look right. Question: How can I make MDCSelect:change work without affecting the UI ap ...