The V-if directive seems to be stuck and not reflecting changes when the variable is

I'm attempting to design a feature in my application that enables a button to trigger the visibility of several other elements and automatically remove itself after being clicked. Below is the relevant HTML code:

<div id="app">
    <button @click="reveal" v-if="!showlists">Start</button>
    <ul v-if="showlists">
    <list v-for="name in chosenNames" v-bind:name="name"></list>
    </ul>
</div>

Here, the unsorted list should only be displayed when the variable "showlists" is set to true, while the button should disappear once "showlists" becomes true. The structure of my Vue app is as follows:

let app = new Vue({
    el: "#app",
    data: {
        showlists: false,
        chosenNames: [
        { text: "name1" },
        { text: "name2" },
        { text: "name3" },
        ]
    },
    methods: {
        reveal: function() {
            showlists = true;
        }
    }
})

Initially, the "showlists" variable is initialized as false, and the program operates as expected with the button visible and the list hidden. However, upon clicking the button, the function executes and changes "showlists" to true (this was verified during troubleshooting). Despite this, the DOM fails to update dynamically and retains its original state.

I apologize if this issue appears to be quite basic; I am relatively new to Vue and constantly striving to enhance my understanding :)

I would highly value any assistance provided.

Answer №1

Remember to include the "this" keyword in your "reveal" method before setting the showlists variable to true in your Vue component.

Here is an example of how you can structure your code:

<div id="app">
    <button @click="reveal" v-if="!showlists">Start</button>
    <ul v-if="showlists">
    <list v-for="(name, index) in chosenNames" :name="name" :key="'list-'+index"></list>
    </ul>
</div>

And here is a snippet for creating a new Vue instance:

let app = new Vue({
    el: "#app",
    data: {
        showlists: false,
        chosenNames: [
        { text: "name1" },
        { text: "name2" },
        { text: "name3" },
        ]
    },
    methods: {
        reveal: function() {
            this.showlists = true;
        }
    }
})

Hopefully, this solution will address your issue. Happy coding! :)

Answer №2

There are 4 bugs in your code:

  1. The correct usage of v-bind is to set element's attribute, not innerHTML
  2. Change showlists to this.showlists
  3. showlists = true; is always being set to true
  4. Instead of using the tag list, you need to use li. Below is the corrected code:
<div id="app">
    <button @click="reveal" v-if="!showlists">Start</button>
    <ul v-if="showlists">
    <li v-for="name in chosenNames" v-html="name"></li>
    </ul>
</div>
<script>
    let app = new Vue({
    el: "#app",
    data: {
        showlists: false,
        chosenNames: [
        { text: "name1" },
        { text: "name2" },
        { text: "name3" },
        ]
    },
    methods: {
        reveal: function() {
            this.showlists = !this.showlists;
        }
    }
})
</script>

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

Exploring Inner Textures on a Cylinder in Three.js

I am brand new to Three.js and am currently using it for a small feature on my website. My goal is to create a 3D model of a paper cup, with different textures on the outer and inner sides. So far, I have been able to achieve some rotation functionality ...

Enhancing IntelliJ IDEA's autocomplete functionality with JavaScript libraries

Is there a way to add my custom JavaScript library to IntelliJ IDEA 10.5 or 11 for autocomplete functionality? I want to specify that IDEA should recognize and suggest auto-completions for objects from my library. While it sometimes works automatically, ...

Angular 2 issue with nested form elements

Looking to build a form that includes nested sub/child components within it. Referring to this tutorial: https://scotch.io/tutorials/how-to-build-nested-model-driven-forms-in-angular-2 https://plnkr.co/edit/clTbNP7MHBbBbrUp20vr?p=info List of modificatio ...

Setting up TypeScript and SCSS as the default languages for scripting and styling Vue components in Vite: A comprehensive guide

I recently developed a project utilizing Vue 2.8 and Vite. In order to incorporate TypeScript and SCSS, I found myself manually specifying them in each component: <script lang="ts"> <style scoped lang="scss"> Since the pr ...

Essential Input Element in HTML5

I'm currently facing an issue with form validation that involves two text boxes, both of which are optional. However, in order for the server to validate the form, one of the text boxes must be filled in. I have no problem with creating a required fie ...

What methods can be used to ensure the accuracy of the device time and prevent any tampering with it? I am seeking ways to implement this feature on both Android and iOS devices

After conducting some research, I've come up with a few thoughts on this issue. One approach is to implement custom logic to verify the accuracy of the device time (I am using Firebase for backend). Another option is to check if the device's Aut ...

Implementing the rendering of HTML stored in an array in Angular

I have an array that includes an object with HTML elements which I would like to render in Angular. Here is the array: { name: "rules", key: "rules", value_before: "<tr><td>revisit_in_some_days</td><td>less_then</td>td> ...

Transmitting JSON information from AngularJS to Node.js

Having trouble sending JSON data from AngularJS frontend to Express Node.js? Here's what I've tried: frontend.html <form ng-submit="func()"> <textarea name="inputtext" type="text" ng-model="sentence"></textarea> </form& ...

Encountering a problem while working on a Javascript scaffolding application build utilizing Angular 2

I'm having trouble getting this code to run. 'use strict'; /** * @ngdoc function * @name udaciMealsApp.controller:MenuCtrl * @description * # MenuCtrl * Controller of the udaciMealsApp */ angular.module('udaciMealsApp') .c ...

When I include the <object> tag in a Vue template, what occurs?

I encountered an unusual issue in my Vue project. Here is the outcome: https://i.sstatic.net/2g2PH.png This is the code snippet: <template> <div class="container"> <main class="messages"> <message ...

I'm feeling a bit lost on how to bring my random quote generator to life

I'm attempting to add animation to my random quote generator by making it look like another card is being flipped on top of the existing one, similar to a deck of cards. With limited coding knowledge, I followed a tutorial and made some adjustments to ...

The jQuery validation feature permits entering a code that falls within the range of user1 to user100

Here is an example where the user can only enter a code between 1 and 100. Otherwise, it will return false. var regexCode = /var regexEmail = /^0*(?:[1-9][0-9]?|100)$/; $(document).on('change','#code',function() ...

How does selecting one dropdown option within a dynamic form field lead to the automatic activation of another dropdown list in a separate form field?

My goal is to create a dynamic form field with a dropdown list that allows users to add and delete multiple contributors. Currently, my code can successfully add and remove multiple users. However, I encounter an issue where selecting the dropdown list tri ...

Transform the array by utilizing its own elements

I came up with a solution to a problem I was facing, but I'm not entirely comfortable with it. It feels risky to me, like using an array to redefine itself is not a good coding practice. It's similar to trying to explain a word by using the word ...

Dynamically changing video sources with JavaScript

I have created a web cam recorder using this code. While it works well, I am looking to add an additional feature where users can record multiple videos and easily switch between them. To implement this, I modified the JavaScript file with the following c ...

Eliminating repeated keys from an array of objects

I'm dealing with an array of objects that looks like this let array = [ { sector: "adad" }, { sector: "adadasdsd" }, { sector: "sdsdsd" }, { origin: "sdfsf" }, { destination: "dfsfsdf" } ]; My goal is to trans ...

Struggling with a character entity in Javascript? Learn how to escape it and avoid any display issues (such as showing

document.title = ("welcome &rarr; farewell"); Trying to display the arrow symbol "→" but it's not showing correctly. Any tips on how to properly escape it? ...

Exiting the window event

While searching the internet for a solution, I discovered that the method I thought was the simplest way to handle a window being exited (window.unload()) has been deprecated in an earlier version of jQuery. Does anyone know of a straightforward approach t ...

Why isn't my computed property functioning properly in Vue.js?

Exploring the code snippet provided below: new Vue({ el: '#app', computed: { myMessage: function() { return "hello"; } }, data: { message: this.myMessage }, mounted: function() { console.log(this.myMessage); ...

Incorporate variable key-value pairs into a JavaScript array or object

Is it possible to add a key value pair to an existing JavaScript associative array using a variable as the key? I need this for JSON encoding and prefer a simple solution over using plugins or frameworks. ary.push({name: val}); In the above code snippet, ...