On JQuerys’s page (http://api.jquery.com/submit/) I am trying out one of their examples. It’s pure copy paste but (except, html, body, etc.) Still it doesn’t work. What is the problem:
<body>
<html>
<head>
<script>
$('#target').submit(function() {
alert('Handler for .submit() called.');
return false;
});
</script>
</head>
<form id="target" action="get_input_values.html">
<input type="text" value="Hello there" />
<input type="submit" value="Go" />
</form>
<div id="other">
Trigger the handler
</div>
</body>
</html>
I just want to have a function that can be triggered when I submit a form. Next step I will need to get the values from the input fields.
Thanks in advance!
You’ll want to attach the submit event to the form once the form has loaded on the page.
To do this you can wrap the whole thing in a
$(document).ready():This is basically saying that once the document has finished loading, go and find the form and attach a submit event to it. Trying to do this the way you did it runs the risk that the form on the page won’t have loaded by the time your Javascript tried to find it.
Here’s a working example of your code: http://jsfiddle.net/MmMa8/