1

I try change color of div by clicked on him, but the event not responded, what I'm doing wrong? here is my code

HTML:

<div class="demo" @click="attachRed != attachRed" :class="{ red: attachRed }"></div>

script: '

export default {
  data() {
    return {
      attachRed: false
}}

' style:

.demo {
  width: 100px;
  height: 100px;
  background-color: gray;
  display: inline-block;
  margin: 10px;
}
.red {
  background-color: red;
}

1 Answer 1

2

It is

@click="attachRed = !attachRed

Not

@click="attachRed != attachRed

The != symbol is a test operator, and is read as "not equal," so what you have is "attachRed is not equal to attachRed." You should use the assignment operator of just = and negate the right-hand value. It should read "attachRed is assigned to NOT (!) attachRed."

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

2 Comments

thanks! I just solved the stupid mistake:)
Don't worry it happens to everybody. I am confused my answer helped you?

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.