Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove class in vue.js

As I am new to Vue.js, can anyone help me in how to remove class from element similarly like we do in JQuery.

$('.class1').removeClass("class2");
like image 539
Ayaz Sayyed Avatar asked Nov 20 '18 14:11

Ayaz Sayyed


People also ask

How do I remove a class from a tag?

To remove a class from an element, you use the remove() method of the classList property of the element.

What is class binding in Vue?

Binding to Objects The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive . You can have multiple classes toggled by having more fields in the object. In addition, the :class directive can also co-exist with the plain class attribute.

How do I add a class to Vuejs?

Adding a dynamic class name is as simple as adding the prop :class="classname" to your component. Whatever classname evaluates to will be the class name that is added to your component. Join 11,067 other Vue devs and get exclusive tips and insights delivered straight to your inbox, every week.


2 Answers

From what is written in their documentation I'd say it something you should not do in your code.

Instead, your CSS classes should be bounded to properties and presence of a class should be determined by property value.

Example (from docs):

<div v-bind:class="{ active: isActive }"></div>

The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive (if isActive IS true - class will be there).

You can have multiple classes toggled by having more fields in the object. In addition, the v-bind:class directive can also co-exist with the plain class attribute. So given the following template:

<div class="static"
     v-bind:class="{ active: isActive, 'text-danger': hasError }">
</div>

And the following data:

data: {
  isActive: true,
  hasError: false
}

It will render:

<div class="static active"></div>

When isActive or hasError changes, the class list will be updated accordingly. For example, if hasError becomes true, the class list will become static active text-danger.

I believe that's the right way to go :) Please check the documentation for more details.

If for some reason you need to remove a class you could add jQuery as a dependency to your app and use it (but it's not preferable).

Happy hacking :)

like image 177
ikos23 Avatar answered Oct 17 '22 06:10

ikos23


Given you have a this element

<div id="randomDiv">
  <p :style="{backgroundColor:color}" @click="updateBackgroundColor(color)">{{obj.name}}</p>
</div>

the :style allows you to add a style to the element, in this case it is the backgroundColor style we are adding, it can be anything, and you can see that has a value of color which comes the Vue object bellow. This is initially set to yellow in the vm = new Vue() object. This object also has a function called updateBackgroundColor which carries out the update. The color is passed into this function from the @click in the element.

var obj = {
  "name": "Curtis",
}


var vm = new Vue({
  el: '#randomDiv',
  data: function (){
    return  {
        obj,
        color: 'yellow',
    }
  },
  methods: {
        updateBackgroundColor: function (color) {
        console.log(color);
          if(color === "yellow"){
            this.$set(this.$data, 'color', 'red');
          }
          else{
            this.$set(this.$data, 'color', 'yellow');
          }
        }
    }
});

i have also pasted this in JsFiddle for you to see.

[https://jsfiddle.net/ifelabolz/todqxteh/1044/][1]

like image 43
eagercoder Avatar answered Oct 17 '22 06:10

eagercoder