Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recognize the backspace and spacebar with jQuery

when a user clicks the spacebar or backspace I need to create a variable that stores the action so I can use it to manipulate an array. I want the backspace to delete a value in an array and the spacebar to create a space. How can I do this?

like image 906
Michael Rader Avatar asked Nov 22 '11 06:11

Michael Rader


People also ask

How does jQuery detect keyboard press?

The keypress() method in jQuery triggers the keypress event whenever browser registers a keyboard input. So, Using keypress() method it can be detected if any key is pressed or not.

How do you call a spacebar in Javascript?

keyCode = 32; // 32 is the keycode for the space bar document.

What is the keycode for spacebar?

the keyCode=49 for a space.

What is keycode in jQuery?

Introduction to jQuery keycode Key codes are the keyboard keys to which have digital values mapped to the keys based on the key code description. jQuery keycode is a part of Themes in jQuery UI API category, there are many more API's like disableSelection(), enableSelection(), . uniqueId(), . zIndex(), .


2 Answers

Maybe this can help you:

$('body').keyup(function(e){    if(e.keyCode == 8){        // user has pressed backspace        array.pop();    }    if(e.keyCode == 32){        // user has pressed space        array.push('');    } }); 
like image 149
Saeed Neamati Avatar answered Sep 22 '22 22:09

Saeed Neamati


try this

$(document).ready( function() {         $('#inputid').bind('keypress', function(e) {             if (e.which == 32){//space bar                 alert('space');             }             if (e.which == 8) {//backspace                 alert('back space');             }     });       }); 
like image 38
run Avatar answered Sep 25 '22 22:09

run