Delfino
Delfino

Reputation: 1009

Vue.js - Change element's class based on change to data property

I have a Vue instance which has two data properties: error which is initially set to false, and classArray which is an object that contains two classes: btn and btn-success.


btn is set to true and btn-success is set to error, which initially is false.

I have two input elements, both of which are buttons, where the first one's class is set to classArray.

The other button, upon clicking, invokes a method attached to my Vue instance that is supposed to toggle error (so if error is true, then it becomes false, and vice versa).

My expectation is that, because btn-success in classArray is set to the value of error, that upon toggling the value of error the corresponding class should be active on my first element.

Even though the toggling of error works as expected, when inspecting the first element, it doesn't appear that the btn-success class was added.

Is there something here I'm missing, or can you not add classes to elements like this?

Also, here is the code I'm using to test this:

var app = new Vue({
  el: '#app',
  data: {
    error: false,
    classArray: {
      btn: true,
      'btn-success': this.error
    }
  },
  methods: {
    toggle: function() {
      this.error = !this.error;
      console.log(document.getElementById('input1'));
    }
  }
});
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<div id="app">
  <input type='button' id='input1' :class='classArray' value='Submit' />
  <input type='button' @click='toggle' value='Change class' />
</div>

Upvotes: 1

Views: 5798

Answers (1)

Khauri
Khauri

Reputation: 3863

The problem is that the value of btn-success is set to true only once when the data object is first created and doesn't change after that so changing this.error won't have any effect on classArray. Instead you could set classArray as a computed property and it will update itself whenever this.error is updated.

var app = new Vue({
  el: '#app',
  computed : {
    classArray(){
      return {
        btn : true,
        'btn-succes' : this.error
      }
    }
  },
  data: {
    error: false,
  },
  methods: {
    toggle: function() {
      this.error = !this.error;
      console.log(document.getElementById('input1'));
    }
  }
});
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<div id="app">
  <input type='button' id='input1' :class='classArray' value='Submit' />
  <input type='button' @click='toggle' value='Change class' />
</div>

This is just personal preference, but I personally like to use inline classes in this style (spacing for emphasis).

<input 
    type='button' 
    id='input1' 
    :class='["btn", error && "btn-success" ]'
    value='Submit'/>

Doing it this way means you can avoid adding tons of computed properties when you have a lot more elements that need variable classes.

Upvotes: 4

Related Questions