Attempting to create a Chrome extension, I use jQuery to change certain input
and textarea
values using the following statement:
$(this).val(newValue).trigger('change');
The goal of the trigger
part is to notify any 2-way data binding modules used on the current page that the value of the input
has been changed.
It does not work.
I tried with Knockout and Rivets, and both do not know that the value of the input
has been changed using the above jQuery line.
Using the accepted answer here, I tried:
$(this).val(newValue).change();
That does not work either.
Interestingly enough, if I downgrade to pure DOM interface (i.e. no jQuery), then it works fine and the 2-way bindings get properly notified of the value change. Using the following code:
element.value = newValue;
element.dispatchEvent(new Event('change'));
The above works and all 2-way binding modules that I tried are aware of the value change.
How can I achieve the same effect with jQuery? Note that my code lives in the content script of my Chrome extension.