Dynamically get value of onkeypress input tag in jquery
I want to get a dynamic value from an input tag. 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 the 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 instantly. It now shows the first value in the alert box after pressing the second key. But I want to get the value of the input at the same time. How is this possible.
+3
source to share
1 answer
you need to use the INPUT event . it fires when the user changes the textbox at any time. Hope this 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" />
+3
source to share