Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event for textarea changes

I have a JavaScript library that updates a hidden <textarea> HTML element with some data based on some things.

In my (JavaScript) application I'd like to know when these updates occur without having to go through the existing library code.

I was hoping there was an event for this, but apparently not.

How would I listen for changes of the textarea?

Changes can be as simple as document.getElementById("myTextarea").value = "hello";

EDIT I will be using FireFox only, since this code will only be part of a test suite I'm building.

like image 712
Luca Matteis Avatar asked Jan 15 '10 13:01

Luca Matteis


People also ask

Can we use Onchange for textarea?

The onchange element could be used on a textarea to launch a script after the user finishes filling in the field. The script could do something like enable the form submit button or enable a follow-up field that shouldn't be completed until the textarea has received input.

Which is not event of textarea?

Unfortunately, FF does not fire change event for textarea .

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

$('#textareaID'). on('input change keyup', function () { if (this. value. length) { // textarea has content } else { // textarea is empty } });

What can I use instead of Onchange?

Try using the input event.


1 Answers

If you control the Javascript library you mention, I would say the easiest way would be to manually trigger the change event every time you change a field's value.

I think this is how it's done in JQuery: Events/change Otherwise, a simple document.getElementById('myTextarea').onchange() might work as well.

If you have many calls, you could wrap the changing of the value, and the triggering of the event into a changeFormElement(id, value) function.

This requires, of course, that you can modify every instance of when a textarea is changed. If that is given, this is probably the most elegant way.

like image 154
Pekka Avatar answered Oct 29 '22 22:10

Pekka