javascript - character does not get typed when input has keydown event listener and in debug mode -


i have input keydown event listener. i'm new @ javascript/html maybe obvious when try out in browser every thing works. type character , can see javascript ran , type character , works. when open chrome developer tools , put break point in event listener function when type not appear in input element after event listener finished running. reason this?

here javascript:

$("#author").keydown(function(event) {  }); 

and here html element:

<input class="form-control" name="author" type="text" id="author"> 

so try , explain again: when type character (for example "m") in input element letter "m" shows in input element. when type character, 1 shows up. refresh page. open chrome developer tools , put break point on event listener. type in keyboard "m", , empty event listener runs , there no indication on page typed "m". not showing in input element. when remove debug point works. type character , shows in input element.

here jsfiddle example. if type input box works. if open dev tools (at least in chrome) type , continue js interpreter nothing typed.

with basic typing of letter, there several events fire (you can decide ones want listen to)

so if type letter "m" in text field fire several events, in following order:

  1. keydown
  2. keypress
  3. keyup

if hold down key, repeatedly fire "down" , "press" events.

$("#author").keydown(function(event){    var key = event.keycode || event.charcode;  	$('#events').append('keydown: ' + key + '<br/>');  });  $("#author").keyup(function(event){    var key = event.keycode || event.charcode;  	$('#events').append('keyup: ' + key + ' value:' + $("#author").val() + '<br/><hr/>');  });  $("#author").keypress(function(event){  	$('#events').append('keypress: value:' + $("#author").val() + '<br/>');  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <input class="form-control" name="author" type="text" id="author">  <div id="events"></div>

the trick is, although without debug tools open, see value entered in field, isn't entered until keypress event fires. if have break point set, or never "return" keydown event, press never happens, , field never updates include appended character.


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -