Author -  Sai gowtham

How to toggle a class in Vue

In this tutorial, we are going to learn about how to dynamically toggle a class in Vue.js with the help of examples.

Toggling the classes

We can toggle a class dynamically in vue by passing an object to the v-bind:class attribute.

In this example, we are toggling a class by clicking the Toggle button.

App.vue
<template>
  <div id="app">
    <div v-bind:class="{box: isActive}"></div>    <button @click="handleToggle">Toggle</button>  </div>
</template>

<script>
export default {
  data() {
    return {
      isActive: true    };
  },
  methods: {
    handleToggle() {
      this.isActive = !this.isActive;    }
  }
};
</script>

In the above code, we have passed {box: isActive} to v-bind:class attribute, where box class will be added to div element if the isActive data property is true; otherwise it is removed.

If you click on a Toggle button the box class is removed, and toggle it again box class is added.

You can also pass multiple classes like this.

App.vue
<template>
  <div id="app">
    <div v-bind:class="{box: isActive, error: isError}"></div>    <button @click="handleToggle">Toggle</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isActive: true,      isError: false    };
  },
  methods: {
    handleToggle() {
      this.isActive = !this.isActive;
      this.isError = !this.isError;
    }
  }
};
</script>

It will render:

<div class="box"></div>

After clicking the toggle button:

<div class="error"></div>

Using the Array syntax

Similarly, we can also toggle classes by passing an array to the v-bind:class attribute.

App.vue
<template>
  <div id="app">
    <div v-bind:class="[isActive ? box : '' ]"></div>    <button @click="handleToggle">Toggle</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isActive: true,    };
  },
  methods: {
    handleToggle() {
      this.isActive = !this.isActive;
    }
  }
};
</script>

In the above code, we have used a ternary operator (?) inside the array, where box class is added to div element if an isActive data property is true, otherwise empty string ' ' is added.

You can also pass multiple classes by using an object syntax inside the array syntax.

App.vue
<template>
  <div id="app">
    <div v-bind:class="[{box: isActive}, {error: isError} ]"></div>    <button @click="handleToggle">Toggle</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isActive: true,      isError: false    };
  },
  methods: {
    handleToggle() {
      this.isActive = !this.isActive;
    }
  }
};
</script>

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY