1

The following snippet reproduces the input text in the webpage using simple javaScript and jQuery.

I am wondering, though, how come there is a one character (or more precisely : one keystroke) latency between the input and the output

eg :

  1. I type 'abcde'

  2. the output is 'abcd'

  3. however if I press the Insert key, the ultimate 'e' prints.

My code :

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <input type="text" name="enteredText" id="myTextInput" />

    <p id="myTextOutput">
    blabla 
    </p>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
  $("#myTextInput").keypress(function( ){
    var theText = $("#myTextInput").val();
    $("#myTextOutput").html(theText); 
  });

$( "html" ).click(function( event ) {
 
    var value = $("#myTextInput").val();
    $("#myTextOutput").html(value); 
 
});

    </script>
  </body>
</html>

Any idea ? Thanks

3 Answers 3

2

If you want to get rid of tat latency. use keyup or keydown instead of keypress:

$("#myTextInput").keyup(function( ){
    var value = $("#myTextInput").val();
    $("#myTextOutput").html(value); 
  });

Here is the DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

keydown has the same issue as keypress, at least in Chrome: jsfiddle.net/mmojdaqe
the reason I used keyup in my samples is that.
I know, just pointing it out since you suggest using keydown in the second sentence. :-)
2

The most likely reason is that the keypress event handler is executed before the content of the input field is updated. When you read the content, you still read the old content, not the updated one.

From jQuery's keypress docs:

Note: as the keypress event isn't covered by any official specification, the actual behavior encountered when using it may differ across browsers, browser versions, and platforms.

Using keyup instead fixes the issue:

  $("#myTextInput").keyup(function() {
    var theText = $("#myTextInput").val();
    $("#myTextOutput").html(theText);
  });

  $("html").click(function(event) {

    var value = $("#myTextInput").val();
    $("#myTextOutput").html(value);

  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" name="enteredText" id="myTextInput" />

<p id="myTextOutput">
  blabla
</p>

Comments

0

You're grabbing the value before you've allowed the event to propagate up to where the text field has been updated. You can add an infinitesimal delay to get the full value:

$("#myTextInput").keypress(function( ){
    setTimeout(function() {
       var value = $("#myTextInput").val();
      $("#myTextOutput").html(value);
    }, 0);
  });

2 Comments

Yeah great, this seems to be the fastest responding fix amongst the following solutions : keyup/keydown/Timeout
You should just use the keyup event instead of the keypress with a timeout. That's just silly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.