Khuram Malik
Khuram Malik

Reputation: 1485

Perform function upon pressing enter after editing text in a ContentEditable element

I have a h1 element that I have set to ContentEditable = "true". Within that element, i wanted to restrict the text to one line only and after the first enter is pressed to set focus to another element of the page.

So i've set the css with nowrap and overflow hidden which visually restricts the css to one line. Now on pressing enter, i wanted to set the focus, so i started with the following basic jquery to detect pressing of enter, but the alert doesnt work?

$(document).ready(function() {
 $('.edit').keypress(function(e) {
     if(e.which == 13) {
    alert('You pressed enter!');
}
 });
 })

Upvotes: 3

Views: 1540

Answers (1)

Anoop
Anoop

Reputation: 23208

Use keydown jsfiddle

$(document).ready(function() {
 $('.edit').keydown(function(e) {
     if(e.which == 13) {
        $(this).blur().next().focus();
        return false;
      }
 });
 })

Upvotes: 1

Related Questions