Get Textarea Value Once A Key Pressed
I have an function executed after a keypress event : $('#txtarea').keypress(function(){ alert(document.getElementById('txtarea').value); }); I want to return the new text of t
Solution 1:
Try to use keyup
instead of keypress
Solution 2:
The keypress just get fired before the value assigned. Like every other event(click, submit etc'...) so you can cancel the default behavior and "stuff"
$('#txtarea').keypress(function(event) {
returnfalse; // this will disable the new value assignment
});
You can find out what button was clicked with event.which and work with it.
Example:
<input id="txtarea" />
$('#txtarea').keypress(function(e){
var value = this.value;
alert(value);
alert(value + String.fromCharCode(e.which))
});
You can also use the keyup
event, but it has other meaning and usage than keypress
.
Be aware!
Solution 3:
You can use this code, note keyup rather than keypress as this means the key has been added to the textarea:
$('#txtarea').keyup(function() {
alert(this.value);
});
Also, no need to do the document.getElementById, just use this.value
Solution 4:
Perform the behaviour after a zero-length timeout.
$("#txtarea").keypress(function(){
setTimeout(function () {
alert(document.getElementById("txtarea").value);
}, 0);
});
Post a Comment for "Get Textarea Value Once A Key Pressed"