javascript - Execute event before its natural behaviour -
is there way execute javascript-event before it's natural behaviour?
for example, using code below, if press /
key on keyboard, character printed in input field, , after 2 seconds, alert showed. that's obviuos.
but there way make alert showed before /
printed?
<input></input> <script> document.queryselector("input").focus(); addeventlistener("keydown", function (e) { if (e.keycode === 191) { myfunc(e); } }); function myfunc(e) { settimeout(function() { alert("hello!"); }, 2000); } </script>
unified solution inputted characters on keypress
event:
addeventlistener("keypress", function(e) { var val = e.which || e.keycode; e.preventdefault(); myfunc(e.target, val); }); function myfunc(target, val) { settimeout(function() { alert("hello!"); target.value = string.fromcharcode(val); }, 2000); }
try it.
Comments
Post a Comment