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.
$(this).keyup(function (e) { var params = { 'search_type': search_type, 'q': $(this).val() }; $('#result').text(params.q); });
Comments
Post a Comment