Submit form with jQuery 

Joined:
02/21/2009
Posts:
130

June 22, 2010 19:40:53    Last update: June 22, 2010 19:52:53
Use jQuery to submit a form by clicking a button not associated with the form. The jQuery submit method can also be used to associate an event handler on the form.
<!DOCTYPE html>
<html>
<head>
	<title>jQuery Submit Form</title>
	<script type="text/javascript" 
		src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
	</script>

	<script type="text/javascript">
	$(function() {
		// attach a handler on submit
		$('#myForm').submit(function() {
			alert('Before submit');
		});

		// submit form with onclick
		$('#submit').click(function() {
			alert('Button clicked');
			$('#myForm').submit();
		});
	});
	</script>
</head>

<body>
	<form id="myForm">
		Enter Your Name:
		<input type="text" name="name"><br>
	</form>

	<!-- button outside of form. -->
	<button id="submit">Submit</button>
</body>
</html>


To submit the first form on the page:
$('form:first').submit();

Share |
| Comment  | Tags