Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js - Add Class on Click Event

I would like to dynamically toggle a class on a click event on a div in Vue.js without using properties/data to do so.

Here is my div

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

When this div is clicked, I would like to add the class quiz-item--correct or quiz-item--incorrect (the logic for this will be handled elsewhere). I cannot use properties as there are too many answers in the quiz for it to be a maintainable/viable approach.

Does anyone have any ideas on how I can achieve this functionality?

like image 284
AdamMcquiff Avatar asked Mar 28 '17 10:03

AdamMcquiff


1 Answers

You can do something like this:

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

You can check the fiddle demonstrating this: Here

like image 76
abhishekkannojia Avatar answered Oct 11 '22 10:10

abhishekkannojia