0

I am new to JavaScript and jquery and want to check fields in a form before submitting. But even a simple example taken from How to do something before on submit? does not work. I do not ge an alert in Firefox and do not see any errors in the webdeveloper's console. See the code:

<html>
<head>

<script src="jquery.min.js"></script>
<script type="text/javascript">
$('#myForm').submit(function() {
  alert('Handler for .submit() called.');
  return false;
});
</script>
</head>

<body>


<form id="myForm" action="foo.php" method="get">
   <input type="text" value="" />
   <input type="submit" value="submit form" />
</form>



</body>
</html>

And of course there is "jquery.min.js" in the same folder.

Community
  • 1
  • 1
tardis
  • 1,018
  • 20
  • 37
  • 4
    Make sure to wrap your code in `document.ready` event **or** add the script before the closing `body` tag. – elclanrs Nov 22 '13 at 09:54
  • 1
    You need a `DOM ready handler` to wrap your code with. See [here](http://learn.jquery.com/using-jquery-core/document-ready/) for more information on this. – Nunners Nov 22 '13 at 09:56
  • 1
    I recommend to read [jQuery's documentation](http://learn.jquery.com/about-jquery/how-jquery-works/). There it says: *"To run code as soon as the document is ready to be manipulated, jQuery has a statement known as the ready event"*. – Felix Kling Nov 22 '13 at 10:35

5 Answers5

3

You need to add the script in a dom ready handler

jQuery(function () {
    $('#myForm').submit(function () {
        alert('Handler for .submit() called.');
        return false;
    });
})
Arun P Johny
  • 365,836
  • 60
  • 503
  • 504
1

When the js code is executed, the html element has to already exists. To delay the code strat, use document.ready as above:

<script type="text/javascript">
$(document).ready(function(){
  $('#myForm').submit(function() {
    alert('Handler for .submit() called.');
    return false;
  });
});
</script>

Note: the syntax $('#myForm').submit(function() { is deprecated and should probably be replaced by $(document).on("submit", "#myForm", function() { or something similar :)

EDIT: also for what you ask, see javascript documentation about e.preventDefault(), this could also be useful for what you want :)

Asenar
  • 5,861
  • 2
  • 31
  • 46
1

This will work too, setting script after element already added to the DOM:

<html>

    <head>
        <script src="jquery.min.js"></script>
    </head>

    <body>
        <form id="myForm" action="foo.php" method="get">
            <input type="text" value="" />
            <input type="submit" value="submit form" />
        </form>
        <script type="text/javascript">
            $('#myForm').submit(function() {
                alert('Handler for .submit() called.');
                return false;
            });
        </script>
    </body>

A. Wolff
  • 72,298
  • 9
  • 84
  • 139
1

you should try e.preventDefault(); instead of return false; E.g

$(function() { $('#myForm').submit(function(e) { e.preventDefault(); alert('Handler for .submit called.'); }); });

because some times return false; may not be effective on some codes on jquery

amostk
  • 49
  • 5
0
    <html>
    <head>
    <script src="jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    $('#myForm').submit(function() {
      alert('Handler for .submit() called.');
      return false;
    });
    });
    </script>
    </head>
    <body>
    <form id="myForm" action="foo.php" method="get">
       <input type="text" value="" />
       <input type="submit" value="submit form" onsubmit="submit" />
    </form>
</body>
</html>

demo: http://jsfiddle.net/kapil_dev/6Tf7Y/

kapil_dev
  • 86
  • 4