Answer the question
In order to leave comments, you need to log in
How to add and remove the class of neighboring blocks?
Hello.
I have two buttons. Button1 and Button2.
How can I add the active class to it by clicking on Button1, and remove the Active class from Button2 by clicking on Button2, and add it to Button2 accordingly (Active class). This is the first task.
The second task, there is also a block (Block), I need that by clicking on Button1, for example, the test1 class is hung on Block, and by clicking on Button2, the test1 class is deleted and the test2 class is added instead.
By and large, this should work roughly like tabs, but I could not find a normal simple solution.
For a complete understanding, I sketched the markup .
Answer the question
In order to leave comments, you need to log in
We put the data of buttons and blocks into an array, we also add a property to the component - a link to the active element of the array (this is the button whose button should receive the active class and the class of which should be assigned to the block):
data: () => ({
items: [
{ buttonTitle: '...', blockClass: '...' },
{ buttonTitle: '...', blockClass: '...' },
...
],
active: null,
}),
<button
v-for="n in items"
:class="{ active: active === n }"
@click="active = n"
>{{ n.buttonTitle }}</button>
<div v-if="active" :class="[ 'block', active.blockClass ]">
<button v-for="button in 2" :key="button" @click="active = button" :class="{active: active === button}">button</button>
<div id="block" :class="`test${active}`">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question