[Vuejs]-I want to change the class of my div on text change using vuejs

0👍

Lets making simple.

According to the vuejs documentation this is how you can use class.
https://v2.vuejs.org/v2/guide/class-and-style.html

<script>
export default {
  name: "App",
  data() {
    return {
      data: ""
    };
  },
  computed: {
    changeBackground() {
      return this.data.length > 0 ? "bg-one" : "bg-two";
    }
  }
};
</script>
<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

.bg-one {
  background-color: red;
}

.bg-two {
  background-color: yellow;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<template>
  <div id="app">
    <div :class="changeBackground">
      <input v-model="data" type="text">
    </div>
  </div>
</template>

Look at this sandbox => https://codesandbox.io/s/wonderful-star-ronjr

Leave a comment