Deactivate the unclicked button using Vue.JS

I have a scenario with two confirmation buttons - when one button is clicked, it should disable the other button and reveal the content of a specific span element.

Initially, both buttons should be enabled until the page loads. Additionally, I want to alternate the 'btnClicked' class based on which button is enabled or disabled.

I feel like I'm close to solving this issue, but I'm struggling to identify the next step. As a beginner in learning Vue.JS, I would appreciate any guidance on where I may be going wrong in this code snippet.

var app = new Vue({
  el: '#app',

  data: {
    disabled: 0,
  },
}); 
.btnClicked{
  background: green;
  color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
   <button @click="disabled = (disabled + 1) % 2, $event.target.classList.toggle('btnClicked')"  :disabled="disabled == 1">Yes</button>
   <button @click="disabled = (disabled + 1) % 2, $event.target.classList.toggle('btnClicked')"  :disabled="disabled == 0">No</button>
   
   <span v-if="disabled == 1" class="showifYes">You just clicked Yes</span>
</div>

Answer №1

Is this the kind of thing you're looking for?

let application = new Vue({
  el: '#application',

  data: {
    disabled: null,
  },
}); 
.btnClicked{
  background: green;
  color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
   <button :class="(disabled === 1) ? 'btnClicked' :  ''" @click="disabled = (disabled + 1) % 2"  :disabled="disabled === 1">Yes</button>
   <button :class="(disabled === 0) ? 'btnClicked' :  ''" @click="disabled = (disabled + 1) % 2"  :disabled="disabled === 0">No</button>
   
   <span v-if="disabled == 1" class="showifYes">You just clicked Yes</span>
</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

Incorporating a JSON file through a script element

A customized I18n plugin has been developed to accept various languages through json files. The goal is to simplify usage for users by allowing them to easily insert their json package directly into a page along with the script: <script id="pop-languag ...

Is it possible to dynamically change the 'amount' on a PayPal button or iframe?

My task is to create a checkout page that exclusively uses PayPal for payment processing. The challenge I face is that the purchase amount is not fixed; users can input the desired amount for their purchase (e.g. buying a £100 gift code). PayPal typicall ...

Assigning the variable "name" attribute to an <input> element in HTML: A step-by-step guide

In the image below, I am working on creating a user interface using JSP and loops. I am looking to assign unique names to each input element to differentiate them. To achieve this, I plan to give them variable name attributes which I can set and populate ...

Building a Node.js API using Express and MySQL that incorporates a search parameter functionality, which is applied only when set and allows for a combination of

I am looking to enhance my search functionality by allowing for a partial match on the 'first_name' column. Specifically, I want to be able to search for names that contain the input provided in the URL. Here is an example of the URL that curren ...

Comparing fields within arrays of objects using JavaScript ES6 and higher versions

Feeling stuck in a loop here. The concept seems straightforward, but JavaScript is really giving me a hard time. I have two arrays of objects: let allProfileUsers = [ { id: "0b4cd920-31da-11ec-a31c-cd844bfb73be", auth_user_id ...

Combining res.render and redirect in Express.js for efficient rendering and redirection

I am currently developing a web application that involves setting up routes for user authentication. The Challenge: After a successful registration, I want to redirect the user to /login page while also including render options. However, when I use render ...

Include scrollView on smaller screens based on conditions

While incorporating an overlay in my application, how can I integrate a ScrollView specifically for smaller devices? Initially, I am checking the width: const windowWidth = Dimensions.get("window").width; Subsequently, I am attempting to display the Scro ...

How to retrieve an image from an external source using jQuery

There's a link that looks like this: http://vignette4.wikia.nocookie.net/2007scape/images/c/c1/3rd_age_amulet.png/revision/latest?cb=20141217224859 and I'm trying to figure out how to download it into a specific folder with a specific name usin ...

What could be causing my router link tag to malfunction within the bootstrap framework?

In my project, I am using vue.js in combination with bootstrap to develop a navigation task bar for the frontend. To set up the routing functionality, I have created a separate file called router.js. router.js import Vue from 'vue' import Router ...

How can Express JS be configured to make URL calls from a local environment?

I encountered an issue with my code (Weather App using OpenWeatherMap Api) when I attempted to move my apiKey and apiUrl to the .env file. An error appeared in the terminal, but it's unclear why it occurred. Below is my code: const express = require( ...

Hot reloading functionality ceases to function within Docker container after a period of time

Scenario In our development setup, we utilize Docker to containerize a Vue.js application and attach a volume with the source code for easy testing and maintenance. Docker Configuration FROM node:13.8-alpine RUN yarn install && \ apk ad ...

Having trouble getting Tesseract.js to work with an Electron App built using Vue and vue-cli-plugin-electron-builder

Struggling with using Tesseract OCR in a project created with vue-service-cli? You're not alone. I've been facing issues trying to load lang.traineddata both locally and remotely. Despite attempting various solutions found in tesseract.js repo an ...

Update the content of a div using jQuery and AJAX by targeting a specific button ID

I am in the process of developing a website that functions as an interactive "choose your own adventure" game. The page will present users with a question along with three different choices represented as buttons. Upon selecting one of the options, jQuery ...

Looping through arrays using ng-repeat

{ "employees" : [ { "name" : "XXX", "id" : "1", "Salary" : [ { "Month" : "XXXX", "Amount" : "XXXX", }, { "Month" : "XXXX", "A ...

What methods can Ajax utilize to make asynchronous requests and receive synchronous responses?

While using jQuery ajax to send a request to a web service, I came across an interesting bug: var AjaxResult; login = function () { AjaxResult = ""; $.ajax({ type: "POST", url: KnutBase.getServiceUrl() + "ServiceInterface/HmsPlanne ...

Is there a way to assign a value to a select option without relying on jQuery?

I'm working on converting a jQuery code to vanilla JavaScript $('.city option[value="NY"]').attr('selected','selected'); How can I achieve this? The following code is giving me an error document.querySelector('#ci ...

Tips for retrieving text from an element that is nested within an element's promise in Appium

Here's a snippet of code I'm working with: const wd = require('wd') const driver = await wd.promiseChainRemote("http://localhost:4723/wd/hub") elements = await driver.elementsByAccessibilityId("commonElementsId") I have received a pro ...

Is Client-side Validation Compatible with Uploadify?

I am in the process of developing a webpage that utilizes uploadify for users to upload multiple images. In order to streamline this process, I would like to implement client-side validation using jQuery or JavaScript before submission. Although I am profi ...

Comparing Two Arrays in AngularJS and Disabling on Identical Cases

I currently have two tables: "Available subject" and "Added subject" which are populated by ng-repeat with two separate lists of objects. My objective is to accomplish three things: 1 - When the user clicks on the plus sign, a row with identical content s ...

Having trouble with lodash in InertiaJS + Vue within an event handler causing an error

I'm working on a project with Vue + inertiajs (using Laravel) and I need to implement the ._pull function from lodash within one of my Vue single file components. The default setup in bootstrap.js looks like this: import _ from 'lodash'; wi ...