Implementing Vue.js - Applying a Class on Click Behavior

Is there a way in Vue.js to dynamically toggle a class on a click event for a div without relying on properties or data?

Take a look at this div:

<div class="quiz-item" @click="checkAnswer(1)">

Upon clicking this div, I want to add either the class quiz-item--correct or quiz-item--incorrect (with the actual logic handled elsewhere). Since the quiz has numerous answers, using properties is not a feasible or efficient solution.

Any suggestions on how to achieve this functionality?

Answer №1

If you want to achieve a similar effect, you could try implementing the following code:

<div class="question-item" @click="$event.target.classList.toggle('classname')">

For a demonstration, you can refer to this fiddle: Check it out here

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

Problem with JavaScript in Internet Explorer 8

To ensure a consistent bottom background at all times on the page, I devised a method to determine whether or not the bottom part needed to be pushed down based on the window height and main content height. However, I encountered an issue when the content ...

Creating a plane in Three.js using points along different axes

My goal is to construct a plane that intersects the points (2, 3, 12) on the x, y, and z axes respectively (equivalently, the equation of the plane is 6x + 4y + z = 12). So far, I have attempted to generate a flat plane and rotate it around the three axes ...

Why is my CSS causing a div with 100% width to be slightly taller than expected?

I am working on a website that I want to adjust to the width of different browsers. Everything seems to be functioning properly, except for the fact that the parent div containing the 100% width divs always appears around 5 pixels too tall. This is causin ...

Creative ways to conceal JavaScript within your Wordpress website

After placing my javascripts in the header.php file, I realized that the code could easily be snatched by viewing the source on any post or homepage... Can you recommend a straightforward method to conceal my javascripts in WordPress and prevent them from ...

How can I use CSS to position one div on top of another div, without it moving when the page is scrolled down?

I have two images, one rectangle and one circular. The rectangle image needs to be positioned 10em lower due to a banner image at the top. Half of the circle should overlap with the rectangle while the other half remains outside. The center of the circle m ...

Experiencing difficulties when uploading information to an API in a React JS application

Trying to Send Data via React to Contact Form API Having issues trying to send input data through the API when clicking submit button. The error encountered is that the API call should look like this Example Link However, it calls in a different way Diff ...

Observing the technique in Vue.js 2 does not result in the modification of the isAuth value

Hey, I'm new to Vue.js and I've been working on setting up authentication for a basic site. So far, I have implemented user registration and login functionality. The views are displayed based on the user's authentication status. However, I&a ...

What is the best way to process an API call entirely on the server without revealing it to the client?

Seeking guidance on a technical issue I've encountered. On my website, x.com, a form submission triggers an API call to y.com, yielding a JS hash in return. The problem arises when Adblocker Plus intercepts the content from y.com, causing it to be bl ...

Utilizing NextJS to Call the Layout Component Function from the Page Component

I can't seem to find an answer to this question for Next.js after searching online. While there are solutions available for React, I don't think they will work in the Next.js framework. My application is essentially a shop with a navigation menu ...

"Utilizing D3's linkHorizontal() function to dynamically update based on the

My goal is to implement a drag and drop feature with a connection line in my project. The connection line consists of a starting point ([x, y]) obtained when the mouse is clicked, and a target point ([x, y]) that tracks the mouse position as the element is ...

Converting CSS properties to MaterialUI Styles in ReactJS: A step-by-step guide

In my CSS, I've included the following code: [contentEditable=true]:empty:not(:focus):before{ content:attr(data-text) } This snippet allows me to display a placeholder inside a content-editable div when it is empty. Since I am using Material-UI ...

Error: react-router v4 - browserHistory is not defined

I'm diving into the world of creating my very first React app within Electron (also my first experience with Electron). I have two routes that need to navigate from one to another. Here's the code snippet I am using: Root ReactDOM.render( < ...

Tips for updating a plugin from phonegap 2.5 to the most recent version 3.1, and the steps to follow when including a new plugin in phonegap

$('#sendSms').click(function(e){ alert("Sending SMS in progress");//After this alert, I encountered continuous errors and couldn't determine the root cause var smsInboxPlugin = cordova.require('cordova/plugin/smsinboxplugin'); ...

What are some different CSS options for setting the indentation of the first line of

I've encountered an issue with how the <p> text-indent property appears when a page is printed using Internet Explorer's Active-X plugin. Here is the relevant CSS: p { font-family: Calibri; font-size: 20pt; line-height: 1.75em; margin-bot ...

"JavaScript/jQuery: The pattern in the text does not align with the string

I am currently working on validating a text field with the specific data pattern of "I-MH-ABCD-ABC-1222". Below is the regular expression I have implemented, but unfortunately it is not functioning as intended. var router_added_sap = "I-MH-ABCD-ABC-1222" ...

The navbar links in Bootstrap 4 may be hidden, but the navbar brand still remains visible

I have implemented this code for my website's navbar, however, upon running it, I noticed that the navigation links are not visible. The navbar brand name "test" appears correctly, but the other links seem to be hidden. Does anyone have any insights o ...

Is it possible to adjust the background image with properties like Scale to Fill, Aspect Fit, or Aspect Fill?

Is it possible to set the background image using the following properties in HTML? (These are properties used for iOS images, but I am unsure if there are similar ones in HTML): Scale to Fill, Aspect Fit, Aspect Fill https://i.stack.imgur.com/5X83I.jpg ...

"document.createElement encounters an issue when used for creating a new window

I am currently working with two different HTML files: FirstWindow and SecondWindow. The FirstWindow is linked to FirstWindowJS.js, while the SecondWindow is linked to SecondWindowJS.js. The issue arises when I try to open SecondWindow.html using FirstWind ...

AngularJS - Increase the input date by 6 hours before converting it to JSON

Is there a way to add 6 hours to an input date in AngularJS? I attempted the following code: var eventStart = new Date ($scope.event.startdateid); var startDate = new Date ( eventStart ); startDate.setHours ( eventStart.getHours() + 6 ); event.startdate ...

Is it possible to add data in MongoDB without specifying a field name?

I have a couple of queries that revolve around the same concept: If I want to insert a new 'row' in MongoDB, can I do so by specifying the order of the fields? For instance, if my collection looks like items = { { name: "John", age: "28" ...