5

I want to bind change event to textarea(read only) whenever its value is set dynamically by opening popup window.

I am able to set the value, but the change event is not getting fired.

I used below code to bind change event to textarea :

$('textarea[name="Cordinator"]').bind("change", onChangeCordinator);
function onChangeCordinator(){}
Sheetu
  • 85
  • 1
  • 7

2 Answers2

8

How are you setting the value? By default the change event fires only if the value is changed by the browser user.

If you are setting the value programatically you need to use .trigger('change')

So somewhere in your onclick handler you need:

$('textarea[name="Cordinator"]').trigger('change');
jfrej
  • 4,548
  • 29
  • 36
  • Sorry it may sound wrong...when should i trigger the function?? – Sheetu Nov 15 '12 at 11:35
  • Wherever and whenever you are changing the value. – jfrej Nov 15 '12 at 11:37
  • Actually i am using popup window to get the value...and i don't have access to that code (and cant modify it)...is there anyway around i can trigger that function...maybe using window focus function or some other?? – Sheetu Nov 15 '12 at 11:59
  • You are using the popup to **get** the value. Are you also using it to **set** the value? If so, I presume the popup page is on the same domain? If yes, you can use `onbeforeunload` like here: [capture-the-close-event-of-popup-window-in-javascript](http://stackoverflow.com/questions/9388380/capture-the-close-event-of-popup-window-in-javascript) – jfrej Nov 15 '12 at 12:08
  • yeah i just saw that...thanks for the advice...now testing it... :) – Sheetu Nov 15 '12 at 12:09
3

there is a syntax error in your js

change this to

$('textarea[name="Cordinator"]').bind("change", onChangeCordinator);});

this

$('textarea[name="Cordinator"]').bind("change", onChangeCordinator);

UPDATE:

well you need to trigger it manually after setting the value on textarea like this

$('textarea[name="Cordinator"]').val('Set Your Value Here').trigger('change');

DEMO

rahul
  • 7,573
  • 7
  • 39
  • 53
  • oh sorry...it was a mistake...i m using this line only $('textarea[name="Cordinator"]').bind("change", onChangeCordinator); – Sheetu Nov 15 '12 at 11:32
  • Actually i am using popup window to get the value...and i don't have access to that code (and cant modify it)...is there anyway around i can trigger that function...maybe using window focus function or some other?? – Sheetu Nov 15 '12 at 11:59