Andrei
Andrei

Reputation: 497

Simulate a submit button click via JavaScript

Is it possible to Submit a form with JAVASCRIPT? if yes please help me...

I have a form with input fileds in it. So I want javascript to count if all fields are field in and then press on the submit button.

The submit button "Save" will be hidden from visitors eyes.

<form id="my form" action="">
    <input type="text" name="fname" id="fname" value=""/>
    <input type="text" name="sname" id="sname" value=""/>
    <input type="text" name="email" id="email" value=""/>
    <button type="submit" name="submitAccount" id="submitAccount">save</button>
</form>

here is a opened FIDDLE

Thanks to all for any help!

Upvotes: 0

Views: 2899

Answers (2)

Srinath Mandava
Srinath Mandava

Reputation: 3462

There is a required attribute

<form id="myForm" action="">
    <input type="text" name="fname" id="fname" value="" required/>
    <input type="text" name="sname" id="sname" value="" required/>
    <input type="text" name="email" id="email" value="" required/>
    <button type="button" name="submitAccount" id="submitAccount" onclick="checkForm();">save</button>
</form>

Form gets submitted only if the required fields are filled

Upvotes: 0

Epsil0neR
Epsil0neR

Reputation: 1704

you can submit form via JavaScript even without submit button, form element has method .submit() which submits whole form.

var myForm = document.getElementById('myform');
myForm.submit();

Before submiting form you can get values from every field in form and make you own validation.

P.S. don't use values for id attribute with whitespace, you should rename it to 'myform' or 'myForm'.

Upvotes: 5

Related Questions