1

I have this button that I did in vue component file.

<td>
     <button id="save" type="submit" class="btn btn-primary my-2" v-on:click="added"> Add to Email List </button>
</td>

I want to make this button changed color & text when clicked by user. Example before click, "Add to email list" will be displayed, once clicked "Added" will be displayed instead and the color will be changed (currently in the css the color is blue, and I want to change it to green once clicked). Any help would be greatly appreciated. Thank you!

1 Answer 1

2

Define clicked variable which indicates if button is clicked or not.

<td>
  <button
    id="save"
    type="submit"
    v-bind:class="{ 'btn-danger': clicked, 'btn-primary': !clicked }"
    class="btn my-2"
    v-on:click="added" 
  >
    {{ clicked? "Added" : "Add to Email List" }}
  </button>
</td>
data: () => ({
  ...
  clicked: false
})
methods: {
  added() {
    this.clicked = true;
  }
}
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.