0

I need to pass a VueJS variable to a VueJs function without the page submitting. For some reason the form is still submitting after passing the variable through to the function.

HTML

<div class="list-group-item" v-for="event in events">
    <form method="POST" v-on:submit.prevent="deleteEvent('@{{ event.id }}')")>
        <b><h1>@{{ event.name }}</h1></b>
        <hr>
        <small>@{{ event.description }}</small>
        <hr>
        <b>@{{ event.date }}</b>
        <hr>
        <button class="btn btn-danger form-control">Delete</button>
    </form>
</div>

JavaScript

new Vue({

el: '#app',

data: {
    newEvent: {
        name: '',
        description: '',
        date: ''
    }
},

ready: function(){
    this.retrieveEvents();
},

methods: {

    retrieveEvents: function(){
        this.$http.get('/retrieveEvents', function(response){
            this.$set('events', response);
        });
    },

    addEvent: function(e){
        e.preventDefault();
        var event = this.newEvent;
        this.$http.post('/addEvent', event, function(){
           this.newEvent = {name: '', description: '', date: ''};
        });
        this.retrieveEvents();
    },

    deleteEvent: function(id){
        e.preventDefault();
        console.log(id);
    }

}
});

I don't see why it keeps submitting the form and not passing the variable into VuejJS, everything else works perfectly.

1 Answer 1

1

You don't need to interpolate in the binding syntax, you can access the properties directly:

<form method="POST" v-on:submit.prevent="deleteEvent(event.id)">
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.