Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger change event on textarea in jquery

I try to trigger the event while changing the value in textarea. I bind the change input paste keyup and its works fine when I type the content manually or paste. But I dont know how to trigger when changes occur using jquery. $("#inptxt").val("Jquery");

HTML

<textarea id="inptxt" ></textarea>
<div id="res" ></div>
<input type="button" value="click" id="but" />

jQuery

$("#inptxt").on("change input paste keyup", function() {
   $("#res").html(jQuery(this).val());
});
$("#but").bind("click",function(){
    $("#inptxt").val("Jquery");
});

Example Fiddle

like image 793
Dineshkani Avatar asked Jun 06 '14 06:06

Dineshkani


People also ask

How can I bind to the change event of a textarea in jQuery?

$('. detect-change') . on('change cut paste', function(e) { console.

How to trigger change event in jQuery?

jQuery change() Method The change event occurs when the value of an element has been changed (only works on <input>, <textarea> and <select> elements). The change() method triggers the change event, or attaches a function to run when a change event occurs.

What is onchange in jQuery?

The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the checked state has been changed.

Does jQuery Val trigger change?

val() doesn't trigger change() in jQuery.


2 Answers

Set a value per script does not trigger a change event. If you need the event you must manually trigger it.

$("#but").bind("click",function(){
    $("#inptxt").val("Jquery").trigger('change');
});

jsfiddle

like image 110
Benjamin P. Avatar answered Oct 19 '22 20:10

Benjamin P.


Browser doesn't recognize the text updation within the input field #inptxt, as it is done programmatically using jquery. so you have to manually trigger change event after updating the text value.

Jquery Code:

$("#inptxt").on("change input paste keyup", function() {
    $("#res").html(jQuery(this).val());
});
$("#but").bind("click",function(){
    $("#inptxt").val("Jquery").change();
});

Live Demo:

http://jsfiddle.net/dreamweiver/ZVcG4/6/

Reference SO Question :Why does the jquery change event not trigger when I set the value of a select using val()?

Happy Coding:)

like image 4
dreamweiver Avatar answered Oct 19 '22 22:10

dreamweiver