jQuery events are the actions that can be detected by your web application. They are used to create dynamic web pages. An event shows the exact moment when something happens. These are some examples of events. A mouse click.
The input event fires when the value of an <input> , <select> , or <textarea> element has been changed. The event also applies to elements with contenteditable enabled, and to any element when designMode is turned on. In the case of contenteditable and designMode , the event target is the editing host.
$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.
Occurs when the text content of an element is changed through the user interface.
It's not quite an alias for keyup
because keyup
will fire even if the key does nothing (for example: pressing and then releasing the Control key will trigger a keyup
event).
A good way to think about it is like this: it's an event that triggers whenever the input changes. This includes -- but is not limited to -- pressing keys which modify the input (so, for example, Ctrl
by itself will not trigger the event, but Ctrl-V
to paste some text will), selecting an auto-completion option, Linux-style middle-click paste, drag-and-drop, and lots of other things.
See this page and the comments on this answer for more details.
oninput
event is very useful to track input fields changes.
However it is not supported in IE version < 9. But older IE versions has its own proprietary event onpropertychange
that does the same as oninput
.
So you can use it this way:
$(':input').on('input propertychange');
To have a full crossbrowser support.
Since the propertychange can be triggered for ANY property change, for example, the disabled property is changed, then you want to do include this:
$(':input').on('propertychange input', function (e) {
var valueChanged = false;
if (e.type=='propertychange') {
valueChanged = e.originalEvent.propertyName=='value';
} else {
valueChanged = true;
}
if (valueChanged) {
/* Code goes here */
}
});
Using jQuery, the following are identical in effect:
$('a').click(function(){ doSomething(); });
$('a').on('click', function(){ doSomething(); });
With the input
event, however, only the second pattern seems to work in the browsers I've tested.
Thus, you'd expect this to work, but it DOES NOT (at least currently):
$(':text').input(function(){ doSomething(); });
Again, if you wanted to leverage event delegation (e.g. to set up the event on the #container
before your input.text
is added to the DOM), this should come to mind:
$('#container').on('input', ':text', function(){ doSomething(); });
Sadly, again, it DOES NOT work currently!
Only this pattern works:
$(':text').on('input', function(){ doSomething(); });
EDITED WITH MORE CURRENT INFORMATION
I can certainly confirm that this pattern:
$('#container').on('input', ':text', function(){ doSomething(); });
NOW WORKS also, in all 'standard' browsers.
As claustrofob said, oninput is supported for IE9+.
However, "The oninput event is buggy in Internet Explorer 9. It is not fired when characters are deleted from a text field through the user interface only when characters are inserted. Although the onpropertychange event is supported in Internet Explorer 9, but similarly to the oninput event, it is also buggy, it is not fired on deletion.
Since characters can be deleted in several ways (Backspace and Delete keys, CTRL + X, Cut and Delete command in context menu), there is no good solution to detect all changes. If characters are deleted by the Delete command of the context menu, the modification cannot be detected in JavaScript in Internet Explorer 9."
I have good results binding to both input and keyup (and keydown, if you want it to fire in IE while holding down the Backspace key).
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