javascript - Preventing default behavior for tab stops listeners from working -


i trying dynamically create input field , implement tab autocomplete feature. unfortunately, whenever listen tab , call preventdefault on keydown event stop tab focusing on other fields, keypress listener cannot register tab.

var element = document.createelement("input");   //assign different attributes element. element.setattribute("type", "text"); element.setattribute("value", ""); element.setattribute("name", "test name"); element.setattribute("spellcheck", "false"); element.setattribute("style", "width:400px"); element.classlist.add('text');   element.onkeydown = function(e) {     if(e.keycode == 9){         e.preventdefault();     } }  element.onkeypress = function(e) {     if (!e) e = window.event;     var keycode = e.keycode || e.which;     if (keycode == 13){         // code     } else if(keycode == 9) {         console.log("detected"); //never printed console     }   } 

how can stop tab doing default behavior while adding own functionality?

keypress gets fired after keydown, , since prevent default behaviour in keydown, think prevents keypress event being fired. should try putting in same event handler, or handlers on same event, might tricky, since nothing guarantees order in gets executed.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -