18

I have the following code:

$(document).on('keyup', 'p[contenteditable="true"]', function(e) {
    if(e.which == 13) {
        e.preventDefault();
        $(this).after('<p contenteditable = "true"></p>');
        $(this).next('p').focus();
    } else if((e.which == 8 || e.which == 46) && $(this).text() == "") {
        e.preventDefault();
        alert("Should remove element.");
        $(this).remove();
        $(this).previous('p').focus();
    };
});

I would like to prevent the default action when a key is pressed. preventDefault works for keypress but not keyup. Is there a way to prevent the defualt for $(document).on('keyup')?

2 Answers 2

48

No. keyup fires after the default action.

keydown and keypress are where you can prevent the default.
If those aren't stopped, then the default happens and keyup is fired.

Sign up to request clarification or add additional context in comments.

6 Comments

Any one knows a reference to back this up?
@tutuDajuju It's pretty clear imo, even without a reference. If you hook into keyup then you can't "intercept" the actual key press anymore, since it has already been processed by keydown.
Is there any distinction between saying "keyup fires after the default action" and just saying "keyup's default action cannot be prevented" ? Does the specification specifically dictate which order these actions happen in, or does it simply say that one action isn't affected by the other regardless of which happens first? Does it not technically matter either way?
@OOPSStudio If you tap a key (that being to press and release said key), the sequence is "key down", "keypress", "keyup". "keypress" is deprecated, but the order was specified in the W3C UIEvents spec (still listed in legacy section 9.3.2). There is a distinction to be made. First, it is expected that each event has a default action. The default I was referring to was the particular one that the poster was asking about (cancelable during keydown or keypress, not keyup; "beforeinput" would be correct today). But there is no default action for keyup; it's a notification that the action happened.
Ah, thank you. I assumed "the default action" in your answer was referring to keyup's default action - but you were referring to the default action of the other events. Thank you for clarifying!
|
8

We can prevent the action by using following code snippet.

e.stopPropagation();
      e.preventDefault();  
      e.returnValue = false;
      e.cancelBubble = true;
      return false;

keyup fires after keydown/keypress. we can prevent default action in anyone of those events.

Thanks,

Siva

2 Comments

It is not the proper answer for the asked question otherwise I would upvote it; however I really appreciate your efforts for providing this useful information.
dont use keyup , but use keydown and add his answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.