Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery / Contenteditable - new paragraph on keypress works on original but not generated content

I have some contenteditable paragraphs and have written up some jquery to generate a new paragraph when the enter key is pressed.

$('p[contenteditable="true"]').keypress(function(e) {
    if(e.which == 13) {
        e.preventDefault();
        $(this).after('<p contenteditable = "true">New Paragraph</p>');
        $(this).next('p').focus();
    }
});

This works perfectly well for all paragraphs that existed on page load. It however doesn't work at all for the newly generated paragraphs. How can I get this working on the generated paragraphs as well as the original?

http://jsfiddle.net/UU4Cg/1/

like image 897
Ryan King Avatar asked Nov 28 '25 03:11

Ryan King


1 Answers

For dynamic content use .on

$(document).on('keypress', 'p[contenteditable="true"]', function(e) {
    //code here
});
like image 148
tymeJV Avatar answered Nov 30 '25 17:11

tymeJV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!