How to fetch data from text field without using any buttons?

369 views Asked by At

user enter's the 5digit number into the text field and without pressing any button it must be refelected on the simulator. I know how to write the data into simulator I just wanted to know how to fetch this user data as soon as he enters 5 digits without clicking any button that is, on pressing enter the data in the text field must be fetched.

1

There are 1 answers

0
M H On

YOU ASKED: "I just wanted to know how to fetch this user data as soon as he enters 5 digits without clicking any button that is, on pressing enter the data in the text field must be fetched."

HERE IS HOW, using the keypress event. I also limited the text input to 5 characters as well with maxlength="5".

$(document).ready(function() {
    $("input").keypress(function(e) {
        var key = e.which;
        if (key == 13) // the enter key code
        {
            if ($("input").val().length == 5) {
                //you get the value with $("input").val()
                alert($("input").val());
            }
        }
    });
});
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
Enter 5 characters: <input type="text" id="input" maxlength="5">