2

(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

Zebra
  • 3,858
  • 9
  • 39
  • 52

1 Answers1

1

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/>');
    }
});
Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
  • I forgot to add that Im using contentEditable. this function now disabled ALL key events before I even append html – Zebra Dec 30 '10 at 21:23
  • my bad. I placed the preventDefault() method before the if() statement which disabled every key event. Your code worked perfectly. Thanks Alot! – Zebra Dec 31 '10 at 10:07