views:

30

answers:

2

I have an event to capture when the return key is pressed on a form input field, but the ajax call is not fired, instead the from is submitted as a normal http request.

$("#addurl").keypress(function(e){
        switch(e.keyCode){
            case 13:
                // add url ajax call
                $("body").html(ajax_load)
                .load(loadUrl,{url:$("#addurl").value},function(responseText){
                    $("#videos").append(responseText);
                }
                return false;
        }
    });

...

<form><input id="addurl" name="addurl"/></form>

The case 13 statement was fired correctly when I tested with alert("hi") so why is the ajax call not taking the place of a normal full-blown http request?

+2  A: 

you can just use

$(document).ready(function(){
//capture the form submission event
$("#formID").submit(function(){
   //do something
   return false;  
});

});

melaos
A better method but I accepted the other answer as it accomplishes explicitly what I asked, cheers
rutherford
+1  A: 
$("#inputBoxId").keypress(function(e){            
        if(e.keyCode==13)
         {
            e.preventDefault();  /// add prevent default
            //your code here

         }
    });
Praveen Prasad
I think you'd want to move the `preventDefault()` into the `case 13:` block -- you know, so that the data the user enters actually gets entered.
Stan Rogers
@stan: you are correct, i just missed that
Praveen Prasad
hmm didn't actually work for me though - same result
rutherford