P
P
pegas2018-02-25 15:49:47
Vue.js
pegas, 2018-02-25 15:49:47

How to add a class to the clicked element?

<div class="block-calculations__panel" v-for=" (i, index) in arr ">
        <span :data-col="index" @click="isActive = !isActive" v-bind:class="{ active: isActive }"></span> 
    </div>

How to make sure that not all spans have a class, but only the one that is pressed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2018-02-25
@1PeGaS

Instead of a generic isActive , add this property to each element in arr , and change it:

<div v-for="(n, i) in arr" class="block-calculations__panel">
  <span
    :data-col="i"
    :class="{ active: n.isActive }"
    @click="n.isActive = !n.isActive"
  ></span>
</div>

UPD. Oh, so you want only one element to be active... Rename isActive to just active, this will be the index of the clicked element. Assign a class if the current index is equal to active:
<div v-for="(n, i) in arr" class="block-calculations__panel">
  <span
    :data-col="i"
    :class="{ active: active === i }"
    @click="active = active === i ? null : i"
  ></span>
</div>

D
Dima Polos, 2018-02-25
@dimovich85

<span @click="method">

...
  methods: {
      method(e){
         e.target.classList.add('isActive');
   }
}

In theory it is possible.
Example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question