jQuery: keydown replicating text one character behind -
the objective of this jsfiddle snippet display in <p/>
text entered in <input/>
every time user keys in character. works exception <p/>
1 character behind. what's wrong code?
the jquery code:
var $input = $('<input />'); $('body').append($input); var $p = $('<p/>'); $('body').append($p); $input.keydown(function() { $p.text ($input.val()); });
keydown
fires key pressed, , before value updated pressed key. may wish use keyup()
instead:
$input.keyup(function() { $p.text ($input.val()); });
or simplify to:
$input.keyup(function() { $p.text (this.value); });
you manually append last character (which works, long last keypress isn't backspace or delete:
$input.keydown(e){ $p.val( this.value + string.fromcharcode(e.which)); });
but, frankly, starts become silly when have account special characters (using shift, ctrl, alt or backspace , delete).
Comments
Post a Comment