Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - get value from textbox at every keypress

I have a textbox and I want to use the data in it every time something is entered; letter by letter.

What is happening is that when a value is entered, the Javascript is being executed before the value is actually put into the textbox, meaning that it always lags one character behind.

$(document).ready(
      function() {
          $('#test').keypress(

          function() {
            var value = document.getElementById('test').value;
            alert(value);
          });
      })

<input id="test" type="text"  />

Here's whats happening:

input alert
  w    ""
  e    "w"
  a    "we"
  l    "wea"
  t    "weal"
  h    "wealt"

Whereas I want it to happen dynamically; i.e. when I enter "w" I want the alert to contain "w" immediately after.

like image 628
user2450099 Avatar asked Aug 13 '13 11:08

user2450099


1 Answers

keypress happens before the change, yes. keyup happens after. Listen to that instead.

like image 197
David Hedlund Avatar answered Oct 02 '22 01:10

David Hedlund