I'm doing some kind of screen keyboard where you click in some shapes and letters are written on a textarea. However I want it to work with the physical keyboard as well.
After writing the text into the textarea. I want to save the text on a string and print it.
Well it is working with the shapes, but with the physical keyboard, for some reason, it doesn't recognize the first character written. Only the second one. Thank you!
Demo: http://jsfiddle.net/bcorreia/e49v18bh/2/
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg width="60px" height="60px">
<rect width="50" height="50" data-character="a" />
</svg>
<svg width="60px" height="60px">
<rect width="50" height="50" data-character="b" />
</svg>
<textarea id="text"></textarea>
JS
function getText() {
var search_id = $("textarea#text").val();
console.log(search_id);
var dataString = 'search='+ search_id;
if(search_id!='') {
console.log("enter");
};
}
$(function() {
$('svg rect[data-character]').click(function() {
var character = $(this).data('character');
$('#text').val(function(i, old) { return old + character; });
getText();
});
$('#text').keypress(function(e) {
var character = String.fromCharCode(e.which);
getText();
});
});