0

I have a little problem with Firefox and forms.

I have a form that is dynamically loaded from an external file from the same server trough an XMLHttpRequest(); that has no set target and no direct submit button but sends its data trough a Javascript function, looks like this:

<form name="blahform">
    <input type="text" name="blubb">
    <input type="button" value="Barfoo" onclick="return someFunction(this.form);">
    <input type="hidden" name="id">
</form>

The problem is that Firefox sends this to the forms page, completely ignoring my Javascript code of course. It works if i don't press enter but use the button directly, but i want him to ignore the enter key completely or at least only call the Javascript routine and not try to send the whole thing into nirvana, reloading the page. (And yes, there is a XMLHttpRequest(); waiting behind that Javascript function for that data. ;) )

So, how to tell Firefox to do what i want and not what he thinks is best?

BTW, i have started the form with "submit" instead of "button" and changed to "button" in the hope that this solves the issue, but no luck with that.

EDIT: Solution, thanks to Mike and Riateche: Used an onsubmit="return false;" inside the < form >-tag and it works like expected now.

BastetFurry
  • 299
  • 3
  • 18
  • Is it just Firefox that exhibits this behaviour? Have you checked the error console to see if there's a JS error inside `someFunction`? – Graham Jun 05 '13 at 22:09
  • 2
    By default, if there is no target, it will submit to the same page. You can prevent the default submit action by using an `onsubmit` function. The answers to this question should help you: http://stackoverflow.com/questions/3350247/how-to-prevent-form-from-being-submitted – Mike Jun 05 '13 at 22:16

1 Answers1

2

You need to use the <form>'s onsubmit event instead of onclick event of buttons.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127