Connor
Connor

Reputation: 4168

Make Up/Down arrow in input boxes not do anything

How can I make <input> elements not react to pressing the Up arrow (keyCode 38) or the Down arrow (keyCode 40), while they are focused? I'm using jQuery for the project, but have no qualms against writing it in raw JS if that's easier.

Upvotes: 1

Views: 409

Answers (1)

realshadow
realshadow

Reputation: 2585

Like this:

$('.yourinputclass').keypress(function(e) {
    if(e.which == 38 or e.which == 40) return false; // or you can use e.preventDefault(); like it was mentioned in the comments
});

Documentation here

Upvotes: 6

Related Questions