javascript - Make 'enter'-key run a function from an input type="text" - Stack Overflow

So, I'm having a small problem here. I have a textfield (not area) and I want a JS function to be

So, I'm having a small problem here.

I have a textfield (not area) and I want a JS function to be run when I press enter. This is instead of having a separate 'submit' button. Right now I'm trying to use the 'onKeyPress' event, but that runs the function every time I type one letter in. So if I want to write 'LIKE', it posts 'L' then 'LI' then 'LIK' and then 'LIKE'. It reacts every time I press anything, not only when I enter.

I do not have a form, just a simple input type="text":

<input type="text" id="' + post_id + '" onKeyPress="fbComment(\''+post_id+'\')"> Post a ment </input>

If some of you remember my last question, this will be a part of my facebook_footer variable (I'm trying out the FB API), and the finished thing looks like this:

var facebook_footer = '<button onClick="fbLike(\''+post_id+'\')"> Like </button> <input type="text" id="' + post_id + '" onKeyPress="fbComment(\''+post_id+'\')"> Post a ment </input>';

Do any of you know what I can do to make this work?

So, I'm having a small problem here.

I have a textfield (not area) and I want a JS function to be run when I press enter. This is instead of having a separate 'submit' button. Right now I'm trying to use the 'onKeyPress' event, but that runs the function every time I type one letter in. So if I want to write 'LIKE', it posts 'L' then 'LI' then 'LIK' and then 'LIKE'. It reacts every time I press anything, not only when I enter.

I do not have a form, just a simple input type="text":

<input type="text" id="' + post_id + '" onKeyPress="fbComment(\''+post_id+'\')"> Post a ment </input>

If some of you remember my last question, this will be a part of my facebook_footer variable (I'm trying out the FB API), and the finished thing looks like this:

var facebook_footer = '<button onClick="fbLike(\''+post_id+'\')"> Like </button> <input type="text" id="' + post_id + '" onKeyPress="fbComment(\''+post_id+'\')"> Post a ment </input>';

Do any of you know what I can do to make this work?

Share Improve this question edited Mar 15, 2013 at 21:43 Igy 43.8k8 gold badges92 silver badges114 bronze badges asked Mar 15, 2013 at 20:35 AleksanderAleksander 2,8155 gold badges38 silver badges59 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

heres a simple function -

textElement.onkeydown=function(e){
    if(e.keyCode==13){
        alert('you press enter')
    }
}

You can detect the enter key within the input:

$("#inputIDHERE").keypress(function(e) {
    if(e.which == 13) {
        alert('You pressed enter!');
    }
});

Sure, you can do that - you'll just need some extra logic in your javascript.

Check out Keyboard Events Tutorial

You just have to peek into the keypress event to see what key was entered. The event object has a keycode or which property that will have a value to correspond to the key pressed, and keycode 13 is the enter/return button.

Additional reference: Javascript Character Codes

Here's what worked for me:

$("#inputIDHERE").bind('keypress', function(e) {
    if(e.which == 13) {
        alert('You pressed enter!');
    }
});

I also added <form></form> around my input tag, though that may not be necessary.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745408769a4626433.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信