Amal lal T L
Amal lal T L

Reputation: 430

Dynamically get value of input tag onkeypress in jquery

I want to get the value from input tag dynamically.Here is my script code-

$(document).ready(function () {
       var v = "";
       $("#upload").keypress(function () {
           v = $("#upload").val();
           alert("value = " + v);
       });
       $("#upload").keyup(function () {
           v = $("#upload").val();
           alert("value = " + v);
       });
});

And input tag,

<input type="text"  name="amount" placeholder="Enter Your Amount" id="upload" required />  

So when I press a numeric key in this input tag, I want to get the value instantaneously.Now it is showing first value in alert box after the second key is pressed.But I want to get the value of input concurrently.How is this possible.

Upvotes: 3

Views: 1967

Answers (1)

Soft One Global
Soft One Global

Reputation: 175

you need to use INPUT event. it's fire when user change in text box any time. I hope it helps you.

$(function () {
    var v = "";
    $("#upload").on('input', function () {
        v = $(this).val();
        console.log("value = " + v);
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text"  name="amount" placeholder="Enter Your Amount" id="upload" />

Upvotes: 3

Related Questions