1

I'm trying to create a JQuery method to tell me what the user writes in a text input. For example, if there's an input with the text foo and the user types in b I want to catch that. If the user types a afterwards, I want to catch only that a and not the ba.

How can I do that?

EDIT: I did it using this code:

$(".writetags").each(function(){
        var elem = $(this);
        elem.bind("keyup", function() {
            elem.data("oldVal", $(this).data("newVal") || "");
            elem.data("newVal", $(this).val());
            var oldVal = elem.data("oldVal");
            var newVal = $(this).val();
            console.log("Was "+oldVal+" is "+newVal);
            var newChar = newVal.charAt(newVal.length-1);
            var oldChar = oldVal.charAt(oldVal.length-1);
            console.log("The user just typed in "+newChar+" instead of "+oldChar);
        }); 
    });
5
  • 1
    just attach a function to the keypress event of the input element and get the pressed key from the event object Commented Jan 5, 2016 at 8:18
  • trap the keypress event Commented Jan 5, 2016 at 8:18
  • @FrebinFrancis but how would I detect the new character written in the input? Commented Jan 5, 2016 at 8:21
  • Please post your solution as an answer to your question Commented Jan 5, 2016 at 8:47
  • @TobyAllen I already did, look below Commented Jan 5, 2016 at 12:05

3 Answers 3

1

this is a generic code that listen for all text input Changes in your web page:

 $(document).ready(function() {
    $("input[type=text]").change(function() {
        $(this).data("oldVal", $(this).data("newVal") || "");
        $(this).data("newVal", $(this).val());
        console.log($(this).data("oldVal"));
        console.log($(this).data("newVal"));
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Thanks everyone, but I managed to do it using this code:

$(".writetags").each(function(){
        var elem = $(this);
        elem.bind("keyup", function() {
            elem.data("oldVal", $(this).data("newVal") || "");
            elem.data("newVal", $(this).val());
            var oldVal = elem.data("oldVal");
            var newVal = $(this).val();
            console.log("Was "+oldVal+" is "+newVal);
            var newChar = newVal.charAt(newVal.length-1);
            var oldChar = oldVal.charAt(oldVal.length-1);
            console.log("The user just typed in "+newChar+" instead of "+oldChar);
        }); 
    });

Comments

1

use the keyCode from the keypress event and use String.fromCharCode to get the character:-

$('input').keypress(function(event){
 var character = String.fromCharCode(event.which || event.charCode || event.keyCode);
 console.log(character);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" />

Comments

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.