0

I found this article (http://www.developertutorials.com/tutorials/javascript/javascript-form-submission-no-button-050412-1292/), but it is 8 years old (c. 96 in Internet and duckbilled Platypus years).

strager's answer here: Submitting a form by pressing enter without a submit button indicates that it might be as easy as calling "this.form.submit();" in a selection's event (onChanged() or whatever).

Is that so? Or not so? Or so not so?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

2 Answers2

1

Yes, you can submit the form using .submit() no need of submit button

Html :

<form action="some.php" method="get">
  First name: <input type="text" name="fname">
  Last name: <input type="text" name="lname">
</form>

JS:

$('form').submit();

Demo ---> http://jsfiddle.net/PqXWJ/19/

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
  • Are you sure that's the right jsfiddle? It seems much more involved and not necessarily related. – B. Clay Shannon-B. Crow Raven Jun 17 '13 at 20:45
  • @ClayShannon , Actually i updated one of the fiddle i was working on - anyway, you can see this demo `--->` http://jsfiddle.net/PqXWJ/19/ – Adil Shaikh Jun 17 '13 at 20:47
  • I get, "{"error": "Please use POST request"}" Changing the jQuery to post, it seems to work. Strangely, I also experimented with changing the action. With action set to "/" and using ".submit()", the results pane becomes a complete jsfiddle page in a pane. – B. Clay Shannon-B. Crow Raven Jun 17 '13 at 20:56
1

It's true. You can generally call submit() on a form on any event, as long as you identify the form clearly. @pXL uses jQuery syntax for that; my only caution is that you need to make certain there's only one form on the page, because jQuery fires the handler on the list of elements its selector returns. Either name the form

$('form[name=theOne]')

or use an Array accessor

$('form')[0]

You can make it fire on keyUp in a field (you could test the key value to match 'return'), or even if the user clicks somewhere outside the form.

$('html').click(function(){$('form')[0].submit()});

(This is probably not the best user experience, though!)

Neil JS Grump
  • 669
  • 5
  • 3