In my form I add one text box ($("#seltxt")) and one div having text inside. when I select the div text comes in a text box this works fine. Now I added function keypress in the #seltxt in key press event I am copying the textbox text to div. But it getting one less character.
Example:
div having text “manojgangwar” when I am clicking on div ,”manojgangwar” copies to #seltxt. Now suppose I write character “manojgangwar123” in textbox then in div it is only coming “manojgangwar12”
Below is the jQuery to capture keypress event:
//function to set text to div
$("#seltxt").keypress( function(event) {
$('#' + objid).text($("input#seltxt").val());
});
The keypress event is fired while a key is pressed, before the default behaviour has occurred. This allows scripts to cancel the default behaviour (e.g. populating an input field) through the
event.preventDefault()method.Use the
keyupevent, if you need an event which is fired when the input value has changed.Also, since
$("input#selText")points to itself, you can also use$(this).val()instead. A further optimisation would consist of usingthis.value, which is universally supported.A final note on key events
keydown– Fired once, right after a key is pressed. Can be used to prevent the first default event.keypress– Fired possibly multiple times, while keys are pressed. Can be used to prevent default behaviour (keydownis fired only once, if you hold key pressed, the default behaviour would occur, unless you define akeypressevent)keyup– Fired after the key is released. Usingevent.preventDefault()cannot prevent text from being modified by keyboard input.