I need to capture typing before it is typed into a textbox and edit it to my needs. I write in javascript and jQuery.
I have a text box, and I want to detect when the "#" character is typed - before it is written into the text box - and then capture all the typing that comes afterwards (also before it is written) until I get another "#".
I have seen many examples online on how yo use "keypress" to detect when specific key is typed, but I don't know how to capture all the text that's being typed between the 2 special keys before it is written into the textbox.
I can use any help on the topic!
in this first case the input is prevented for all characters and then inside the text var you can edit and then assign a value to the input field
$('#typehere').on("keypress",function(e){
e.preventDefault();
if(e.which == "#".charCodeAt(0)){
if(listening){
listening= false;
//do what you want with the text
alert("no more listening: "+text);
}else{
listening = true;
alert("listening");
}
return true;
}
if(listening)
text+= String.fromCharCode(e.which);
});
try the fiddle https://jsfiddle.net/42fns28a/
in the second case you can type in the input field until your special char is pressed, you type and edit what you want until the special char is pressed the second time
$('#typehere').on("keypress",function(e){
if(e.which == "#".charCodeAt(0)){
e.preventDefault();
if(listening){
listening= false;
//do what you want with the text
$(this).val($(this).val()+text);
alert("no more listening: "+text);
}else{
listening = true;
alert("listening");
}
return true;
}
if(listening){
e.preventDefault();
text+= String.fromCharCode(e.which);
}
});
second fiddle https://jsfiddle.net/2cdjoq6u/