1

I am new in web development i am trying to handle a form submit using jquery and javascript.

My JS

$(document).ready(function () {

 $('#UnitFrom').submit(function (e) {
 alert(); //Works for Chrome and IE... But not in firefox...
 e.preventDefault(); // Form Is not submitted in Chrome and IE.. for firefox it submits the from to same url that i am in..
  });

 });

My HTML

<html>
<body>
<form id="UnitFrom">
 <input type="submit" id="CalculateUnit" value="Submit"  />
</form>

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="script.js"></script>
</body>
</html>

Any alternate to handle form submitting.

Shanker Paudel
  • 740
  • 1
  • 9
  • 21

3 Answers3

2

Put something in your alert:

alert("TEXT HERE"); 

If you check your console, you should see an error similar to this:

Not enough arguments [nsIDOMWindow.alert]

When the exception is thrown, the method execution ends without calling preventDefault, allowing the form to submit. This is why it is a best practice to place preventDefault at the top of your function, but it is not required. If it is first and there is an exception, the default behavior is still prevented.

James Montagne
  • 77,516
  • 14
  • 110
  • 130
2

You should write:

 $('#UnitFrom').submit(function (e) {
    e.preventDefault(); // prevent default form submission
    alert('some'); // do some stuff
 });

OR

 $('#UnitFrom').submit(function () {
    alert('some');
    return false; // prevent form submission
 });
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • 1
    @ShankerPaudel You should accept this answer or JamesMontagne's. The **real** problem is that Firefox **requires** an argument passed to `alert`, so an exception is thrown and the `e.preventDefault();` doesn't execute – Ian Aug 06 '13 at 18:50
  • I am not so reputed to Vote! :) Sorry about that. – Shanker Paudel Aug 06 '13 at 19:04
  • Should be noted that `return false` will also `stopPropagation` in addition to `preventDefault`. – James Montagne Aug 06 '13 at 19:31
-1

Dude you can not have alert() first and than e.preventDefault();.

e.preventDefault(); has to be first and alert() second.

jsfiddle : http://jsfiddle.net/mpatm/

James Montagne
  • 77,516
  • 14
  • 110
  • 130
pregmatch
  • 2,629
  • 6
  • 31
  • 68
  • Thanks Alot Works with $('#UnitFrom').on("submit",function (e) Not With this.. $('#UnitFrom').submit(function (e) Is there any technical Difference between these two?? – Shanker Paudel Aug 06 '13 at 18:36
  • 4
    That's just incorrect. Of course you can have them in any order. The problem is that Firefox **requires** an argument passed to it, so an exception is thrown and the `e.preventDefault()` doesn't execute. It doesn't mean it can't come second – Ian Aug 06 '13 at 18:38
  • Sorry this is working too .. Thanks $('#UnitFrom').submit(function (e) { e.preventDefault(); // prevent default form submission alert('some'); // do some stuff }); – Shanker Paudel Aug 06 '13 at 18:39
  • @ShankerPaudel `.submit(func...` is just a shortcut for `.on("submit", func...`. There's a limited number of shortcuts, while `.on()` allows you to listen for any event type – Ian Aug 06 '13 at 18:40
  • 1
    Proof that you **CAN** have the `alert` before `preventDefault` if your alert doesn't throw an exception: http://jsfiddle.net/nHkhg/ – James Montagne Aug 06 '13 at 19:35