e.preventDefault();not working (javascript)

Hi iam trying to stop spacebar key from scrolling page but its not working. Please see the pen.

I guess “keyup” is a bit too late. Try “keydown”.

hi, its working with keydown ,but y its not working with keyup??

change to “keyup” to “keydown”

“keydown” is the key being pressed down.
“keyup” is that key being released.

Why do you think keyup won’t do what you want? What happens when you press the spacebar on a webpage

hi,
so you mean to say that page scroll is associated with keydown not keyup?

Well, what happens when you press the space key down on a webpage? Does the page scroll? If so, that’s a hint that the keydown event might be the one

page is scrolled when we press space

thanks actually i was following a video tutorial and she used key up but when i used it problem occur , but thanks its working now