0

I'm new in VueJS and I need a example how to switch color between two buttons when click them.

For example: when I click the white button it turns black and the other one turns white, and vice versa.

2
  • you can use conditional classes on buttons. Please Check documentation. Commented Jun 28, 2022 at 19:28
  • Understand. But my problem lies in the fact that I am not able to do both buttons. I can only change the color of one button Commented Jun 28, 2022 at 19:49

3 Answers 3

2

In your template:

<div class="button-wrapper" :class="{'one-is-active': state.oneIsActive}">
  <div class="button one" @click="toggleButton()" />
  <div class="button two" @click="toggleButton()" />
</div>

In your setup (This is just a suggestion, you could also use a ref or something else; may differ by version.):

const state = reactive({
  oneIsActive: true,
});

function toggleButton() {
  state.oneIsActive = !state.oneIsActive;
}

Your SCSS (may differ a bit in pure css or sass):

.button-wrapper {
  .one {
    background: white;
  }
  .two {
    background: black;
  }
  &:not(.one-is-active) {
    .one {
      background: black;
    }
    .two {
      background: white;
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

If I understand correctly, you want to have functionality similar to a radio button group, where if you click on one, you want the color of the other one to revert.

In order to do that, the buttons need to share state.

Where/how this state is shared can be implemented many different ways, but for this example I'll suggest it is stored in the parent component. The state can also be implemented using composition API, or options API, I will assume based on the level of experience that options may be a better fit.

<script>
export default {
  data() {
    return {
      selected: 0
    }
  },
}
</script>

<template>
  <button :class="{red:selected == 1}" @click="selected = 1">
    Button 1
  </button>
  
  <button :class="{red:selected == 2}" @click="selected = 2">
    Button 2
  </button>
</template>

<style>
  .red{
    background: red;
  }
</style>

example in vue SFC playground

Comments

0

Try this :

new Vue({
  el: '#app',
  data: {
    buttons: [{
      name: 'Button 1',
      bgColor: 'black',
      color: 'white'
    }, {
      name: 'Button 2',
      bgColor: 'white',
      color: 'black'
    }]
  },
  methods: {
    updateColor() {
      this.buttons.forEach(btn => {
        btn.bgColor = (btn.bgColor === 'black') ? 'white'  : (btn.bgColor === 'white') ? 'black' : btn.bgColor
        btn.color = (btn.color === 'white') ? 'black'  : (btn.color === 'black') ? 'white' : btn.cColor
      });
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div v-for="(b, index) in buttons" :key="index">
    <button
            :style="{ 'color': b.color, 'background-color': b.bgColor }"
            @click="updateColor">
      {{ b.name }}
    </button>
  </div>
</div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.