javascript - Capturing key event for backspace -


i having difficulty capturing backspace key keyboard event in javascript/jquery. in firefox, safari, opera, chrome, , on iphone/ipad, capture keyup event on text input box this:

$(id_input).keyup(function(event) {    that.gethints($(this).val().trim(), event, fieldname); }); 

this event captures user keystrokes, sends them function issue ajax lookup call.

my problem comes when user wishes backspace on character he/she typed. in browsers have access except droid phone, when press backspace key, keyup event captures value returned $(this).val().trim() , sends on process in function gethints. on droid, however, neither keyup nor equivalent keydown event fires until user backspaces on every character in $(this).

so, example, if type "cu" backspace on "u" leaving "c" in input field, in browsers except droid, keyup event fire , call function gethints("c", event, fieldname). on droid, keyup event never fires.

what missing? how/why backspace key, on either soft keyboard or hard keyboard, on droid not function expected? how work around this?

you poll changes in text (using setinterval). more reliable. example, keyup wouldn't fire if user right-click -> cut. polling alone less responsive, combine keyup keep snappy. polling bit more processor heavy.

try along these lines:

var oldtext = ''; var timer = setinterval(function(){     var text = $(id_input).val().trim();     if(text != oldtext){         oldtext = text;         that.gethints(text, fieldname);     } }, 500); 

tweak interval duration necessary.

i'm not sure that.gethints event, obviously, wouldn't able pass in using polling approach (because there isn't actual event being fired). problem?

you can use clearinterval(timer); stop polling if want to.

you keep existing keyup function (to increase responsiveness). alternatively, may wish poll avoid that.gethints being called (e.g. if types in quickly).


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -