0

I want to be able to see new values that are entered into a text box using jQuery, one by one (character by character). I don't want the values from the text input, rather I want the value from each event, and not just the amended string from the text input.

<script type="text/javascript">
    $(document).ready(function (e){
        $("#input-text").on("input", function (e) {
            console.log(e.value);    // undefined
        });
    });
</script>

Is there a way of doing this without comparing the old value of $("#input-text") with the new one?

1

1 Answer 1

1

If you want the individual key everytime someone adds a value to the input. just use event.key

$(function() {
  $('input').on('keydown', function(event) {
    var key = event.key;
    if (key.length == 1)
      console.log("Key: ", event.key); //Gives you the individual key
    else
      console.log("Other: ", event.key);
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input />

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

3 Comments

I think that you should restrict the range of the keycodes, because even if I click enter or alt keys, it shows up in the console.
@Kinduser Better? :)
Now it's great.

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.