I noticed prior to posting this question that there have been similar questions posted on this topic before, however the user isn't interacting with the text field by using the keyboard in this instance, in such case binding the text field to the "Paste" action or any of the other nifty suggestions wouldn't work in my case.
Our users are inputting a string value that is scanned in from a Bar Code. What we're attempting to do is to avoid the annoyance of having the user put the scanner down to go to the next field after scanning in the information. However, I'm having an issue detecting the change in value of a text field while it still has focus.
This is the only part of the puzzle that we're missing, because to apply focus to the next field on the form is trivial. Can anyone shed light on how to detect a change in value of a text field when the input device is NOT the keyboard? I've already tried using the change() event but it doesn't fire until the field no longer has focus.
Answer: Use the input Event You can bind the input event to an input text box using on() method to detect any change in it. The following example will display the entered value when you type something inside the input field.
To detect if the element has the focus in JavaScript, you can use the read-only property activeElement of the document object. const elem = document. activeElement; The activeElement returns the currently focused element in the document.
You can just listen for the keypress
event.
<input type="text" id="target" value="" />
var target = $('#target'),
val = target.val();
function monitor()
{
var current_val = $(this).val();
if (current_val != val) {
console.log('changed from', val, 'to', current_val);
val = current_val;
}
}
target.keypress(monitor);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With