html - Jquery element val() not in the right place? -


why jquery report value of search field 1 step behind actual value of field? here in jsfiddle.

<input type="search" class="search" name="test">  <p id="result"></p>  <script>  $('.search').each(function () {     var search_type = $(this).attr('name');     $(this).keydown(function (e) {         var params = {             'search_type': search_type,                 'q': $(this).val()         };         $('#result').text(params.q);     }); });  </script> 

because using keyup, using keydown give behaviour. because keydown fires key pressed, value not updated yet. on keyup value has been updated.

demo

 $(this).keyup(function (e) {     var params = {         'search_type': search_type,         'q': $(this).val()     };     $('#result').text(params.q);  }); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -