1
<button @click="startEffect"> Start Effect</button>
<br/>
<div id="effect" class="highlight" :class="{highlight: enableHighlight}"></div>

I just need the highlight class to be applied based on the data property enableHighlight but for some reason It doesn't apply the class when the startEffect function is called.

<script>
    export default {
        data() {
            return {
                enableHighlight: false
            }
        },
        methods: {
            startEffect: function () {
                this.enableHighlight = !this.enableHighlight;
            }
        }
    }
</script>

I have debugged and confirmed that value of enableHighlight is switched when clicking the button and that the CSS classes are present. However upon clicking the button the class is not applied to the div.

3
  • Why do you have class="highlight"? Commented Apr 8, 2019 at 10:52
  • @JamesCoyle I tried removing that it doesn't make a difference Commented Apr 8, 2019 at 10:56
  • Can you add a snippet that shows the problem? Commented Apr 8, 2019 at 10:58

1 Answer 1

2

You really mess with Vue when having a "normal" class attribute and one dynamic. Remove the normal one. <div id="effect" :class="{highlight: enableHighlight}"></div>

To make it work, you need to remove the function in startEffect's definition:

startEffect() {
    this.enableHighlight = !this.enableHighlight;
}

Why? because this isn't the same in the different ways to define the function. Learn more about this here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.