Preventdefault isn't working


#1

I have this Autoform on a view details page:

{{> quickForm collection="Messages" id="createMessageForm" type="insert" omitFields="createdAt" buttonContent="Submit"}}

and in template events:

"submit form": function(event) {
    event.preventDefault();
  }

When submit is clicked, I get thrown to another page. Could it be that Iron Router is overwriting the preventdefault function?


#2

You might try

"submit form": function(event) {
    return false;
}

instead. From the documentation:

Returning false from a handler is the same as calling both stopImmediatePropagation and preventDefault on the event.


#3

No. Check if the line event.preventDefault(); is even called (put a debugger statement before the line or console.log('IS CALLED')).


#4

return false; does nothing for me. It still takes me back to the list view I have.

console.log('IS CALLED') does fire on submit, so I’m stuck at not knowing how to stay on the current page on submit.

My router config looks like this:

Router.route('/view/:_id', {
  name: 'ViewChat',
  controller: 'ChatsController',
  action: 'view',
  where: 'client'
});

And the route action:

view: function() {
    this.render('ViewChat', {});
  }

#5

I found the documentation for you. It’s the behavior of autoform :wink: https://github.com/aldeed/meteor-autoform/#form-types. event.preventDefault() usually works for normal forms.