3

In my HTML css:

.edit_field {
    height: 50px;
    width: 495px;
    line-height: 3.6;
}

html:

<textarea id="message" class='edit_field'>Enter message here</textarea>

javascrpt:

$('#message').keypress(function(event) {
    // not getting fired this block when I pressed enter key
    // For all other keys its got fired well.
    if(event.which == 13) {
        // -- -- --
     }
});

Is there any problem in my code or in my browser(IE8)?

Jessu
  • 2,069
  • 1
  • 15
  • 19

3 Answers3

5

Try this:

$('#message').keyup(function(e){
  if(e.keyCode == 13){
    //your code probably want e.preventDefault() as well
  }
});

I've found keypress implementation to not be so cross-browser friendly. Just a thought.

Edit: I now remember why this doesn't work in IE8. The enter key (keycode 13) is considered a "Special Key" (as well as others like arrow keys on the keyboard) IE does NOT fire a keypress event for "Special Keys" which is why I had to use keyup.

You can find more info here: http://www.quirksmode.org/dom/events/keys.html

Look under "Special Keys"

Jeremy Battle
  • 1,638
  • 13
  • 18
3

e.which doesn't work in IE try e.keyCode, also you probably want to use keydown() instead of keypress() if you are targeting IE.

See http://unixpapa.com/js/key.html for more information.


jQuery keypress() event not firing?

Community
  • 1
  • 1
Michael
  • 3,416
  • 1
  • 19
  • 21
0

I've encountered this too, particularly with tables using an onKeyDown() in IE7. Other keys fire fine but enter is tricky. I solved mine by moving my onkeydown() trigger about in the HTML, from certain locations it wouldnt pick up enter but would gather others.

If I recall I had it originally in the body tag and moving it to a <div> just inside the body immediately allowed me to detect enter. I was using jsp with a switch but I dont think its a great deal different

celem
  • 404
  • 3
  • 10