K
K
KG2019-09-10 22:04:03
Vue.js
KG, 2019-09-10 22:04:03

How to add/remove a class on click on an element (Vue)?

Hello, not in the dock, not on the Internet, I can’t find how to add / remove a class specifically for this element in Vue by clicking on an element? I have 5 elements, for example, I want that by clicking on one, the 'active' class is added to it and removed by a second click.
I'm trying to do it through :class and a normal method that changes true/false (like in the dock), but I'm getting added/removed on all elements at once. There must be some simple analogue of $(this).add/removeClass('active').

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey delphinpro, 2019-09-10
@pwnography

There must be some simple analogue of $(this).add/removeClass('active').

There is access to the html element:
this.el.classList.add('active')
this.el.classList.remove('active')

But this is generally the wrong approach.
Display in view must be based on data.

D
Dmitry Barkowski, 2019-09-11
@TypeOFF

<div class = "name active" ref = "needed" @click = "$refs.needed.classList.toggle('active')">
</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question