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?
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.
keyCode = 32; // 32 is the keycode for the space bar document.
the keyCode=49 for a space.
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(), .
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('');    } }); 
                        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');             }     });       }); 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With