M
M
myskypesla2018-05-21 20:04:27
JavaScript
myskypesla, 2018-05-21 20:04:27

How to properly remove classes?

Hi all.
There is a ul in which there are 6 lis with the .item.active class.
I need to implement the following in Vue.js, if possible, if not, then in Vanilla JS:
I click on one li, and the .active class is removed from all the others.
I did this, but I don't know if it's right?

<template>
  <ul>
    <li v-for=“i in 6 ref=“li” @click=“addClass”>элемент списка</li>
  </ul>
</template>

<script>
  methods: {
    addClass() {
      const elements = this.$refs.li;
      elements.forEach((item) => {
        item.classList.toggle(‘active’);
      });
    }
  }
</script>

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Evgeny Kulakov, 2018-05-21
@myskypesla

<template>
  <ul>
    <li v-for="i in 6" @click="current = i" :class="{active: current === i}">
      элемент списка
    </li>
  </ul>
</template>

data() {
  return {
    current: 0
  }
}

V
Vladislav Nagorny, 2018-05-21
@esvils

In vue you can use it like this
Documentation here

N
Nikita Dergachov, 2018-05-21
@vanillathunder

<template>
<div>
    <li v-bind:class="{ active: isActive }" @click="setActive(false)"></li>
    <li v-bind:class="{ active: isActive }" @click="setActive(false)"></li>
    <li v-bind:class="{ active: isActive }" @click="setActive(false)"></li>
    <li v-bind:class="{ active: isActive }" @click="setActive(false)"></li>
</div>
</template>
<script>
...
setActive: function(active){
    this.isActive = active;
}
</script>

P
Pavel Kornilov, 2018-05-21
@KorniloFF

How he did it is not clear.
This is how it works in Vanilla .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question