question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Submit a form with javascript doesn't trigger event modifier.

See original GitHub issue

Vue.js version

1.0.23

Reproduction Link

http://jsbin.com/pevimajuro/edit?html,js,output

Steps to reproduce

Submit a form with javascript document.forms[0].submit(); doesn’t trigger event modifier v-on:submit.prevent

What is actually happening?

The event modifier is not triggered. The default event is.

Is this a bug ?

Some web extension auto login after filling form, but don’t seems to work with a vuejs form.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

26reactions
GarethTIGFreightcommented, Dec 1, 2017

I had a scenario where i wanted to submit the form outside of the form and calling this.$refs.form.submit() wouldn’t fire my saveData() function so this is my workaround…

Form HTML

<form ref="form" v-on:submit.prevent='saveData()' v-on:reset.prevent>
    <!-- form inputs --->
</form>
<button @click="saveData()">Save</button>

Vue component method

saveData: function () {
    // Manually trigger validation
    if (!this.$refs.form.checkValidity()) {
        // Try focus on the error 
        this.$refs.form.reportValidity()
        return
    }

    // Actually save code
}

Hopefully this helps someone. Still can be submitted via $refs.form.submit() but I can still manually trigger the Validity (although I’m not sure if reportValidity() is reliable).

8reactions
simplesmilercommented, Aug 6, 2016

@flo-sch adding plain listener won’t help, the problem is that programmatic form.submit() does not fire event at all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Why won't form.submit() trigger the "submit" event?
The form's onsubmit event handler (for example, onsubmit="return false;" ) will not be triggered when invoking this method from Gecko-based applications.
Read more >
Form Input Bindings - Vue.js
When dealing with forms on the frontend, we often need to sync the state of form input elements with corresponding state in JavaScript....
Read more >
type - Cypress Documentation
Submits a form, but does not fire synthetic click event, if there is 1 input and no submit button; Submits form and fires...
Read more >
HTMLFormElement: submit event - Web APIs | MDN
Note: Trying to submit a form that does not pass validation triggers an invalid event. In this case, the validation prevents form submission...
Read more >
Prevent Default and Form Events - Beginner JavaScript
More often than not, you don't want to submit the form to the server but you often want to stop the form from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found