I've looked at numerous other questions and found very simple answers, including the code below. I simply want to detect when someone changes the content of a text box but for some reason it's not working... I get no console errors. When I set a breakpoint in the browser at the change()
function it never hits it.
$('#inputDatabaseName').change(function () {
alert('test');
});
<input id="inputDatabaseName">
In my case, I had a textbox that was attached to a datepicker. The only solution that worked for me was to handle it inside the onSelect event of the datepicker.
Each answer is missing some points, so here is my solution:
The
Input Event
fires on Keyboard input, Mouse Drag, Autofill and Copy-Paste tested on Chrome and Firefox. Checking for previous value makes it detect real changes, which means not firing when pasting the same thing or typing the same character or etc.Working example: http://jsfiddle.net/g6pcp/473/
update:
And if you like to run your
change function
only when user finishes typing and prevent firing the change action several times, you could try this:If user starts typing (e.g. "foobar") this code prevents your
change action
to run for every letter user types and and only runs when user stops typing, This is good specially for when you send the input to the server (e.g. search inputs), that way server does only one search forfoobar
and not six searches forf
and thenfo
and thenfoo
andfoob
and so on.Text inputs do not fire the
change
event until they lose focus. Click outside of the input and the alert will show.If the callback should fire before the text input loses focus, use the
.keyup()
event.onkeyup, onpaste, onchange, oninput
seems to be failing when the browser performs autofill on the textboxes. To handle such a case include "autocomplete='off'
" in your textfield to prevent browser from autofilling the textbox,Eg,
You can use
oninput
event like in jQuery like this:In pure JavaScript
Or like this:
try keyup instead of change.
Here's the official jQuery documentation for .keyup().