Toggle class in Vuejs

Asked

Viewed 20 times

0

I have 3 div with the class ('option') and I have a class as the name of ('Selected'). I would like to remove the class from the div that is already with ('Selected') and switch to the other with the click. I tried so, but he does not withdraw from the last that is already with the ('Selected')

<div @click="e => e.target.classList.toggle('selected')" class="col-4 option">
<div @click="e => e.target.classList.toggle('selected')" class="col-4 option">
<div @click="e => e.target.classList.toggle('selected')" class="col-4 option">
  • Call a function to set a boolean variable. Ai tu faz uma verificação Exp: :class="div ? 'col-4 option': 'col-4'"

1 answer

-2

Using bind you can set the class conditionally.

<div :class="{'selected': foo == 'bar'}"></div>

You could do something like

<div @click="foo = 'option-1'" :class="{'selected': foo == 'option-1' }"></div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.