Reputation: 19787
Why does jQuery report the value of the search field one step behind the actual value of the field? Here it is in a 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>
Upvotes: 1
Views: 41
Reputation: 16922
Because you are using keyup, using keydown will give you that behaviour. Because keydown fires as soon as the key is pressed, but the value is not updated yet. On keyup the value has been updated.
$(this).keyup(function (e) {
var params = {
'search_type': search_type,
'q': $(this).val()
};
$('#result').text(params.q);
});
Upvotes: 1
Reputation: 2005
Use the keyup event. That will get you the updated values.
Here's an explanation for the events: Key Events
Upvotes: 0