Keypress event on inserted span (jQuery)

Posted by Coder on Stack Overflow See other posts from Stack Overflow or by Coder
Published on 2011-02-21T22:46:19Z Indexed on 2011/02/21 23:25 UTC
Read the original article Hit count: 253

Filed under:
|
|
|
|

Hi,

I've been researching for a long time, and haven't found a possible solution. The problem I have is that I'm trying to attach an event handler to an span element just inserted.

So basically the problem is, when a user performs a particular action that I set (clicking a button by example) I call this function:

function AppendSpan()
{
    $('#mydiv').append('<span id="someid" contenteditable="true">Some TExt</span>');
    //Then I want to handle the keypress event on the inserted span
    $('#someid').keypress(function(event){//Do something here});
}

The problem is that the keypress event is never triggered on the inserted span, I tried with the click event and it works fine, but not the keypress or keydown,keyup events. I also tried with $('span').live("keypress",function(event){//Do something}); but didn't work.

Any suggestions would be appreciated, I need this to work only in FF, so no support for IE required.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery