1

I want to click enter instead of submit button and I want jquery to do the exact same task as if I were clicking the submit button.

This is what I have but its not working

the jquery

$("#search-friend-text").keypress(function(e){ // start enter click

    switch(e){

        case 13: 
        $.post("<?php echo site_url('userProfile/search_friend'); ?>", 
        $("#add-friend-search").serialize(),
        function(data){
           $("#insert-activity").html(data);
        });

    $("#add-friend-search").slideUp("slow",function(){});
    break;

    }

}); // end enter click

the Html (form)

<form method="post" action="" name="searchFriendForm" id="add-friend-search">
<input type="text" id="search-friend-text" name="searchFriendText">
<input type="button" class="small green button" id="add-friend-button" />


</form>
1
  • are you able to say where the error occur ? is the post request done ? firebug can tell you that easily. Commented Apr 22, 2011 at 12:54

6 Answers 6

4

use e.keyCode :

switch(e.keyCode) {
. .. 
Sign up to request clarification or add additional context in comments.

1 Comment

+1 and just sharing... either e.keyCode or e.which would work here. I use this page (din.or.jp/~hagi3/JavaScript/JSTips/Mozilla/Samples/KeyEvent.htm) to check myself when I can't remember.
2

First off as Vlad has pointed out, you need to provide the switch with the number and not the event object. Secondly, if you are only going to look for one number, there isn't a need to use the switch/case. Try this:

$("#search-friend-text").keypress(function(e) { // start enter click
    if (e.which === 13) {
        $.post("<?php echo site_url('userProfile/search_friend'); ?>", $("#add-friend-search").serialize(), function(data) {
            $("#insert-activity").html(data);
        });
        $("#add-friend-search").slideUp("slow", function() {});
    }
}); // end enter click

Comments

1

I think this will solve it

switch(e.keyCode) 

Since e is the event handler

Comments

1

You should use the submit event.

$('#search-friend-text').submit(function() {
    // Ajax stuff...
});

1 Comment

No, you've got a fair point. Any code that happens on submit should kept in as a submit event listener, and artificially triggered if an explicit submit isn't performed.
0

The event you receive in your keypress is an event object so i'm not sure your switch/case statement would work anyway! and you can trigger a keypress event for an item by simply invoking $("target").keypress( e ) -- where e is an event you need to build that has the character keycode in it ( you will have to set it in evet.which )

Comments

0

Well you can take a look at this function in jQuery: triggerHandler

But really, it might be better to move your click event code into a separate function which you can call from the click event and from the keypress event.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.