Question

(Im using the before() function from jquery to append a new <p> element to a div layer.

$('#AddParagraphButton').click(function() {
    $('#TheLayer').before('<p contentEditable='true'>Some text...</p>');    
});



here I have set keypress function to insert <br> tag.

$('p').keypress(function(e){
    if(e.which == 13){
       e.preventDefault();  
       document.execCommand('insertHTML', false, '<br/>');
    }
});


this works fine(br tag inserts) until the append function is called and a new <p> is added. How do I get livequery to unbind the keypress event and bind again?


EDIT: the <p> tags have the contentEditable property. Im doing this because the <br> tags are wrapped in divs and I only want <br> tags

Was it helpful?

Solution

Have you considered using the built in live() functionality?..

Description: Attach a handler to the event for all elements which match the current selector, now and in the future.

$('p').live("keypress", function(e){
    e.preventDefault();
      if(e.which == 13){
         document.execCommand('insertHTML', false, '<br/>');
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top