Jitender
Jitender

Reputation: 7971

.submit in javascript vs .submit in jquery

I have a form on page. I want to submit a form when user click on submit button which is outside of form tag. When I am using $('#theform').submit() then submit method work perfectly but when I am doing this document.getElementById('theform').submit() then it is only refreshing my page.

Fiddle

JavaScript

$(function() {
    $('input[type="submit"]').click(function() {
        //$('#theform').submit()
        document.getElementById('theform').submit()
    })

    $('#theform').submit(function(e) {
        alert(0)
        e.preventDefault()
    })
})

html

<form id="theform">
    <input type="text"  id="fname" />
    <input type="text"  id="lname" />
    <input type="text"  id="country" />
</form>
<input type="submit" />

Upvotes: 3

Views: 1375

Answers (2)

Suchit kumar
Suchit kumar

Reputation: 11859

The DOM submit() method does not trigger submit events where as jQuery's does.that is the reason your form in javascript document.getElementById('theform').submit() will submit the FORM.

you can see a post here :Jquery submit vs. javascript submit

Upvotes: 5

Adnan Zameer
Adnan Zameer

Reputation: 782

use document.forms["theform"].submit();

and define action in your form

<form id="theform" action="submit-form.php">

Upvotes: -3

Related Questions