I have an oninput event on a textarea to check the height and resize it. Now I need to edit the value sometimes. I do this just by editting the val() in jQuery, but that does not trigger the oninput event. Is there any way to trigger the oninput event programatically with jQuery?
-
28For future visitors: a simple `.on('input')` works. – hauzer May 24 '14 at 19:22
-
1@hauzer you're absolutely right. Here's a [DEMO](http://jsfiddle.net/yuvii/QU8Sd/1/) that proves it. tested on Chrome, FF & Opera (and I'm guessing there wouldn't be a trouble with modern IE versions) – yuvi Jun 23 '14 at 13:10
-
Or just use simple JS: `element.dispatchEvent(new Event('input'));` (IE9+) – Nux Feb 27 '17 at 14:12
7 Answers
Use .on('input')
. For example:
$('textarea').on('input', function() {
text = $('textarea').val();
$('div').html(text);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea placeholder="Type something here"></textarea>
<div></div>

- 2,646
- 1
- 23
- 21
-
I've accepted this answer instead of the previously accepted one, because these days, this is actually the correct answer. It is now supported and works like a charm. – Deep Frozen Oct 08 '18 at 10:24
-
@paul_jones Thanks man. I always thinking of this. On HTML page we can use – ThataL Nov 16 '18 at 09:19
-
2I'm confused by this answer, since it seems to *handle* the input event, not *trigger* it. @DeepFrozen, why did you accept this answer? – Matthijs Kooijman Feb 28 '20 at 11:45
It is a bit too late, but for future reference, there is the .trigger method.
$("#testArea").on("input", function(e) {
$("#outputArea").text( $(e.target).val() )
});
$("#testArea").val("This is a test");
$("#testArea").trigger("input");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input id="testArea" type="text" />
<div id="outputArea"></div>

- 2,131
- 3
- 27
- 46
You can simply invoke it, e.g.:
$("input")[0].oninput = function () {
alert("hello");
};
$("input")[0].oninput();
...but as @Sammaye points out, jQuery has no explicit "oninput" handler, so you'll have to use POJS.
oninput is not actually in JQuery yet.
You can see posts about it here:
http://forum.jquery.com/topic/html5-oninput-event
http://bugs.jquery.com/ticket/9121
Basically the general consensus is that they don't want it yet.
But no, changing val() directly would not trigger the html5 oninput because it's specification states it is when the user, in the UI, changes the value of the input.
Edit:
However some one has kindly made a plugin for people who wish to use HTML5 only events: https://github.com/dodo/jquery-inputevent

- 47,466
- 33
- 109
- 111

- 43,242
- 7
- 104
- 146
-
1The link to Andy's project is dead. There's a fork which is under active development [on github](https://github.com/dodo/jquery-inputevent). – Joe Taylor Oct 01 '13 at 19:39
-
1@JoeTaylor, you can edit the answer to update the dead link with the new one. By the way, I've already done so. – Denilson Sá Maia Jul 25 '14 at 13:46
-
Maybe this was right when the question was asked. But since jQuery 1.0 there is the trigger method. – Fernando Oct 16 '15 at 18:43
You can bind to input and change:
input will be triggered at user input
change will be triggered at change() and to val(" ") assignments, but after some changes
$("#textarea").bind("input change", function() {
alert("change happend");
});
...
after you binded to change you can call it manualy on each val(" ") assignment:
$("#textarea").val("text").change();
or you can overwrite jQuery val(" ") method to trigger change on each user val(" ") call:
(function ($) { var fnVal = $.fn.val;
$.fn.val = function(value) {
if (typeof value == 'undefined') {
return fnVal.call(this);
}
var result = fnVal.call(this, value);
$.fn.change.call(this); // calls change()
return result;
};
})(jQuery);

- 5,242
- 2
- 39
- 23
Try with "keypress" or "keydown".
Example 1:
$("#textarea").keypress(function(){
alert($("#textarea").val());
});
Example 2:
$("#textarea").keydown(function(){
alert($("#textarea").val());
});

- 549
- 4
- 15

- 2,124
- 1
- 24
- 31
push RUN CODE SNIPPET for seeing results
i've been searching for a better example to join an input range to an input value and so
i modified Fernando's example in a JQuery plugin ,so :
for every
<input type="range" min="1" max="100" value="50" id="range1">
you'll have his value:
<input type="text" disabled id="value" class="range1" value="0">
so is like for any parent range id="range1" there is a child id="value" class="range1"
<!-- <script src="../js/jquery.js"></script> -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
1<input type="range" min="1" max="100" value="50" id="range1"><input type="text" disabled id="value" class="range1" value="0"><br>
2<input type="range" min="1" max="100" value="50" id="range2"><input type="text" disabled id="value" class="range2" value="0"><br>
3<input type="range" min="1" max="100" value="50" id="range3"><input type="text" disabled id="value" class="range3" value="0"><br>
4<input type="range" min="1" max="100" value="50" id="range4"><input type="text" disabled id="value" class="range4" value="0"><br>
...<br>
n<input type="range" min="1" max="100" value="50" id="rangen"><input type="text" disabled id="value" class="rangen" value="0"><br>
<script type="text/javascript">
<!--
$(document).ready(function() {
$('input').on("input",function(){
$('input').each(function(index) {
//console.log('1 '+index + ': ' + $(this).text()+',id='+$(this).attr('id'));
if($(this).attr('id')=='value'){
//console.log('2 class='+$(this).attr('class'));
var obj=$('input#'+$(this).attr('class') );
var hisvalue=$(this);
//console.log('3 parent`s value='+obj.val() );
obj.on("input",function(){
hisvalue.val(obj.val());
});
}
});
});
$('input').trigger("input");
});
//-->
</script>