0

hello I try to grab the submit event, and then I try to pop an alert that says "werks" so I know it works. Note that I have double-checked that the jquery.js file is present in the same folder, and it is the newest jquery.min.js from the jquery website.

When I submit the form, it automatically goes into "get" mode and displays this URL in the URL bar in my browser: "http://localhost/test.html?input=" instead of showing me the alert. I think jquery is trolling me. Full code below:

<!DOCTYPE html> <html> <head> <title>test</title> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $('#form').submit(function(event){ event.preventDefault(); alert('werks'); });</script> </head> <body> <form id="form"> <input type="text" name="input"> <input type="submit" value="Send"> </form> </body> </html> 
1
  • 1
    enclose your code in $(function(){ ... jquery code ... }); Commented Oct 18, 2013 at 7:56

5 Answers 5

1

Try the following code:

<!DOCTYPE html> <html> <head> <title>test</title> <script type="text/javascript" src="jquery.js"></script> </head> <body> <form id="form"> <input type="text" name="input"> <input type="submit" value="Send"> </form> </body> <script type="text/javascript"> $( document ).ready(function() { $('#form').submit(function(event){ event.preventDefault(); alert('werks'); }); }); </script> </html> 
Sign up to request clarification or add additional context in comments.

Comments

0

$('#form') selector returns nothing, since the markup form isn't loaded yet. Move script block after form, or wrap it in $(document).ready(function () { ... }).

Comments

0

You need to put it in a document ready statement:

$( document ).ready(function(){ //Code Here }); 

Comments

0
<script type="text/javascript"> $(function(){ $('#form').submit(function(event){ event.preventDefault(); alert('werks'); }); </script> 

Comments

0

fire your js code on document ready like this:

$(function() { $('#form').submit(function(event){ event.preventDefault(); alert('werks'); }); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.