I have a search that operates by AJAX. It works perfectly well when the user presses the search button, the problem is... if the user presses enter... it submit the form rather than executing the AJAX javascript function. How can I make the Enter button call my AJAX function as opposed to submitting the form?
+1
A:
Trap it in the onSubmit method of the form and return false.
Mech Software
2010-02-08 18:44:59
Sorry, how do I trap onSubmit... can you elaborate?
Jackson
2010-02-08 18:51:11
in your <form> add an onSubmit="return runAjaxInstead()" and have runAjaxInstead() return "false" it won't submit then.
Mech Software
2010-02-08 19:08:01
This also is a framework independent solution if you're not using jQuery.
Mech Software
2010-02-08 19:08:48
A:
With jQuery:
jQuery("#myform").submit(function() {
callAJAXFunction();
return false;
});
Vivin Paliath
2010-02-08 18:49:52
@vivin: imagine you have no idea what jQuery is and re-read your answer... do you think it would help?
Andy E
2010-02-08 18:54:54
It would only take 5 seconds to Google it :)Unless there is a pressing reason, I think directing people towards jQuery is a good idea because it will save them a lot of headache.
Vivin Paliath
2010-02-08 19:00:42
@vivin, I don't think mentioning jQuery is a bad idea, but I do think it's a good idea to explain what it is or link to jQuery.com at the same time :-)
Andy E
2010-02-08 19:08:12
That is a good point :) I will make sure I do that in future responses :)
Vivin Paliath
2010-02-08 19:11:18
It's probably overkill to use jQuery just for this. And jQuery isn't always the best option out there depending on what you do.
JONYC
2010-02-08 19:44:40
@JONYC I disagree. If you have jQuery on hand, I'd say it's better to use it to maintain consistency, than to mix non-jQuery-based DOM-manipulation/event-handling with jQuery-based DOM-manipulation/event-handling.I agree with you that jQuery may not be the best solution for *everything*.
Vivin Paliath
2010-02-08 19:47:41
@vivin I'm just pointing out that doing just that, jQuery is overkill. And plus, the question didn't prompt for jQuery. Also I wouldn't go with jQuery myself because of the poorly designed syntax. Read more about it here: http://blog.thinkrelevance.com/2009/1/12/why-i-still-prefer-prototype-to-jquery
JONYC
2010-02-08 21:33:53
I think that's rather subjective. I'd rather go with jQuery than having to handle crossbrowser issues by myself. For example, I personally prefer jQuery over Prototype, because I don't like how Prototype pollutes the namespace (I noticed you mentioned that in your article, which by the way, is very nicely written!). I agree, the inconsistency sucks - hopefully they will fix it in later versions.
Vivin Paliath
2010-02-08 21:48:22
@vivin I actually didn't write that article but I do program JS at a enterprise level. I have to say Prototype's framework is solid and sick. I used jQuery before but their poorly designed syntax is a deal-breaker for me. Plus, there's nothing jQuery can do that Prototype cannot.
JONYC
2010-02-08 22:12:09
Yes, good syntax definitely helps. But I have found that it far less annoying. To each his own, I guess :)
Vivin Paliath
2010-02-08 22:51:46
+2
A:
Use the form's onsubmit
event to execute your ajax call and make the button into a submit button if it isn't already.
Example HTML
<form action="/search.php" onsubmit="submitAjaxQuery(event)">
<input type="text" name="keywords" />
<button type="submit">Go!</button>
</form>
Example JS
function submitAjaxQuery(event)
{
if (event.preventDefault)
event.preventDefault();
else
event.cancel = true;
// run ajax calling function here.
}
Andy E
2010-02-08 18:50:52
It works, but I think its a little overkill. Rather than an additional function a simple 'onsubmit="ajax(); return false;"' would do the trick
Mark
2010-02-08 21:47:23
A:
Here is a simple DOM way to handle this:
<form action="javascript:void(0)">
<input type="text" />
<input type="submit" onclick="doSomething(this.form)">
</form>
<script>
function doSomething(form){
alert('form submitted');
}
</script>
Place the cursor in the input field, and either if you click the button or type enter, the form is submitted by javscript (not the page)
Mic
2010-02-08 19:39:04
A:
The correct way is to let the non-javascript users use the form submit with page refresh but a javascript call for those with javascript:
<form action="yourscript.php" method="post" onsubmit="doSomething(this.form); return false;">
<input type="text" />
<input type="submit" />
</form>
<script>
function doSomething(form){
alert('form submitted');
}
</script>
JONYC
2010-02-08 19:50:23