LaVOZs

The World’s Largest Online Community for Developers

'; javascript - Detect if a key code corresponds to a key that would input something in a field - LavOzs.Com

Is there a way to determine if a key that is pressed is a key that would actually add something to an input? I.e., change it's value?

For example, if I had an input and I pressed the 'a' key, a letter will appear in the input, however, if I press the down key, the value of the input won't change.

Also, I've tried using a value changed listener but that only fires when I change press a key and then click out of the input, which isn't what I want. I need to use the keydown or keyup listeners and I need to know if the key that was pressed was a key that will actually change the value of the input.

I've tried using a value changed listener but that only fires when I change press a key and then click out of the input, which isn't what I want.

Would an event that fires when the field value changes (at the time the key is pressed) help you?

JQuery:

$('#someInput').on('input', function() { 
    $(this).val() // get the current value of the input field.
});

Vanilla JS:

var input = document.querySelector('#someInput');

input.addEventListener('input', function()
{
    input.value // get the current value of the input field.
});
Related
How do you disable browser Autocomplete on web form field / input tag?
Change the selected value of a drop-down list with jQuery
Why is setTimeout(fn, 0) sometimes useful?
HTML-encoding lost when attribute read from input field
Check if a key is down?
How to detect if the pressed key will produce a character inside an <input> text-box?
jQuery click events firing multiple times
Disabling Chrome Autofill
Detecting Special Key Presses in Javascript
Binding select element to object in Angular