0

I'm trying to make an edit mode when clicking on an "Edit" button. The mode should create a Save-button, a Delete-button and maybe something more.

I'm using jQuery to create the buttons and I'm trying to assign events to them, but the event handler gets fired when I assign it. I've read something about event propagation is the problem, but I can't seem to figure out how to solve it.

Not only does it get triggered, when I try to click the new button, the event is not fired.

Here's an isolated example of my problem: https://codepen.io/anon/pen/ooxQXR

I've tried several methods of interupting the propagation, but nothing seems to work.

e.stopImmediatePropagation();
e.stopPropagation();
e.preventDefault();

Any ideas on how to solve this?

1 Answer 1

4

I would forget about jQuery in this situation - and in most situations when using Vue. Why not use a simple if/else block in the template, like this.

<div id="app">
  <button v-if="!editMode" class="first" @click.stop="clickEvent">First</button>
  <template v-else>
      <button class="save">Save</button>
      <button class="delete">Delete</button>
  </template>
</div>

new Vue({
  el: '#app',
  data() {
    return {
      editMode: false
    }
  },
  methods: {
    clickEvent(e) {
      this.editMode = true;
    }
  },
})

Adding other event handlers to the save/delete buttons should not be a problem then.

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.