Vue.js failing to update in real-time

Within my HTML code, I have the following:

<div class="pl_wrapper">
    <div class="options_pl">
        <input type="button" @click="optionButtonClicked" class="option_button_pl" value="List">
        <input type="button" @click="optionButtonClicked" class="option_button_pl" value="Add a language">
    </div>
    {{show2}}
</div>

In my Vue.js file, I have the following:

const ProgrammingLanguages = new Vue({
            el:".pl_wrapper",
            data:{
                name: "aa",
                show1: false,
                show2: false
            },
            methods: {
                optionButtonClicked(){
                    var indexOfClicked  = index(event.target,event.target.className);
                    var equalIndexOfDiv = getOnIndex(indexOfClicked,"pl_divs")
                    $(".options_pl").hide();
                    show1 = (indexOfClicked==0) ? true : false
                    show2 = (indexOfClicked==1) ? true : false
                }
            }
        });

When clicking on option_button_pl, I expect that {{show2}} will change from false to true and vice versa. However, this is not happening as expected. Here is the Jsfiddle link for reference: https://jsfiddle.net/3akfzcyf/

Answer №1

To reference the current instance, utilize the "this" keyword. For example, you can access functions like this.show1 and this.show2.

Answer №2

Check out the updated code snippet below with added functionality: I have made some adjustments to your code and included the this statement as well as the event parameter inside the optionButtonClicked function.


const ProgrammingLanguages = new Vue({
    el:".pl_wrapper",
    data:{
        name: "aa",
        show1: false,
        show2: false
    },
    methods: {
        optionButtonClicked(event){
            var indexOfClicked = index(event.target, event.target.className);
            this.show1 = (indexOfClicked == 0) ? true : false;
            this.show2 = (indexOfClicked == 1) ? true : false;
            console.log(this.show2);
        }
    }
});

function index(element,className){
    var allElements = document.getElementsByClassName(className);
    for (var i = 0; i < allElements.length; i++) {
        if(allElements[i] == element){
            return i;
        }
    }
}

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

AngularJS ng-repeat - cascading dropdown not refreshing

I'm dealing with an AngularJS issue where I'm trying to create a cascade dropdown like the one below: <div class="col-sm-2 pr10"> <select class="PropertyType" ng-controller="LOV" ng-init="InitLov(140)" ng-model=" ...

Designing a file upload progress bar with the help of jquery and ajax

I am looking to implement a progress bar for uploading files utilizing jquery and ajax. Here is the jquery code I have written: function updateProgress(evt) { // evt is an ProgressEvent. if (evt.lengthComputable) { var percentLoaded = ...

Utilize the filtering functionality in AngularJS to narrow down the search results

I have a table filled with data and I am trying to filter out any rows that have a datetime value that is not current (today's date) or alternatively, change the background color of those specific rows. I have attempted the following code but it is no ...

Issue with Laravel Vue search-dropdown and other components failing to render correctly for certain users

We are currently implementing a searchable dropdown menu for our web application using a vue component. Strangely, the component fails to load on my local version (xampp) as well as on the deployed website. However, it displays properly on another develope ...

State of loading getServerSideProps in Next.js

Can we implement a loading state similar to when retrieving data on the client-side? I'm interested in having a loading state, maybe with a loading-skeleton like react-loading-skeleton On the client-side, we can achieve this by: import useSWR from & ...

JavaScript's Ajax request seems to be stagnant and inactive

Having some difficulties with the code below. It triggers an alert correctly, but the ajax part doesn't seem to be functioning. No errors or indications of what's wrong. $(document).on('change', '.department_select', function ...

Variability in Focus Behavior while Opening a URL in a New Tab with window.open()

Here is a code snippet I have been using to open a URL in a new tab: window.open(urlToOpen, '_blank', 'noopener noreferrer'); The issue I am experiencing is that when this code is executed for the first time, it opens the URL in a new ...

Intermediary Server Facilitating Video Negotiation in WebRTC Piping

In the process of developing a 3-part system for enabling remote vehicles to connect to a base station via WebRTC, my goal is to transmit video from the remote vehicle to a remote station using an RTC Peer Connection. The architecture of this system can be ...

Retrieve the user_id without triggering any route

Is there a way to access the logged in user data without needing to make a request to any route or endpoint? //for example, how can I retrieve the id of the logged in user here? router.get('/',function(req,res,next){ //typically we would acce ...

JavaScript: The Battle of Anonymity - Anonymous Functions vs Helper

I'm currently grappling with a piece of functional style code that is featured in the book Eloquent Javascript: Here's the issue I'm facing: When I have the count() function passing an anonymous function to reduce(), everything seems to wor ...

A guide to accessing and managing events for the child inside a div element using extjs4

How can I dynamically switch between the divs with classes "icon-right" and "icon-left" when clicked? Here is an example of the HTML and ExtJS code involved: Html code: <div class="msg"> <div> <div cla ...

Utilize a pipe to seamlessly transfer data from MSSQL to a Node.js application

I am currently utilizing node along with node-mssql version 6.0.1 for handling large amounts of data retrieval from the database and transmitting it to the frontend using streams. Although I have attempted to implement pipe and stream functionality as rec ...

Error: Vue Loader Unable to find module '@'

Software Version 15.4.0 Link to Reproduction Example https://codepen.io/fendi-tri-cahyono/pen/wbXKMZ?editors=0010 Steps to Recreate the Issue ERROR in ./node_modules/vue-extend-layout/vue-extend-layout.vue?vue&type=script&lang=js& (./node_ ...

Creating a line graph using chart.js and JSON dataset

I'm working on plotting a line graph using a JSON response from my MongoDB, but I keep encountering an error that indicates something might be wrong with my code structure. Here's what I have attempted: myurl = "/api/humidity" $(function() { ...

When a user clicks on an image, I would like to dynamically resize it using a combination of PHP

Although I have a good understanding of CSS and HTML, my knowledge of Javascript is limited. I am looking to add an interactive element to my website where an image enlarges gradually when clicked and smoothly moves to the center of the screen in one con ...

What is the top choice for creating a cutting-edge front-end web module?

Which generator or scaffold is recommended for creating a contemporary front-end web module using npm/webpack? ...

AngularJS routing with html5mode causing 404 error when using htaccess

I am currently working on my very first angularjs application using version 1.6x, and I am encountering some 404 errors with my router. Here is how my router is set up: app.config(function($routeProvider, $locationProvider) { $locationProvider.html5M ...

How to manage UNC paths and "mapped network drives" within an Electron application?

I have developed a cross-platform (macOS-Windows) app using Electron that functions smoothly with files and media assets from a local drive. However, it encounters issues when dealing with UNC paths and "mapped network drives". Since I am a contractor, I d ...

Cutting Out Sections of a List

I'm currently working on an app that involves looking up and navigating to specific locations. I've encountered an issue with the coordinates in my code containing a ',0' at the end, which is not compatible with Google Maps. Manually re ...

Events for the UserManager in Vue.js with oidc-client-ts are failing to trigger

I am currently in the process of developing a Vue3 SPA and experimenting with incorporating the oidc-client-ts JavaScript library for user authentication. While I have successfully integrated the login/logout features with my existing code, I am facing dif ...