Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textarea change dom event that works without losing focus, for example for pasting using a mouse

I am creating a web page for translating texts piece-by-piece. The user is supposed to fill many textareas and press the "save" button when done. The save button is inactive by default, and becomes active on keypress, keyup, and keydown. This, however, doesn't cover the case when text is pasted using a mouse, by right-clicking and pressing "Paste" or by middle-clicking (middle-click pasting is common in X Windows). This pasting scenario is relatively common on the web page that I am creating.

The "change" event would work, but it's only fired after the textarea loses focus. Is there a way to get that mouse pasting event to be triggered immediately when the text changes?

Thank you!

like image 803
Amir E. Aharoni Avatar asked Jan 06 '13 12:01

Amir E. Aharoni


People also ask

What event is triggered when a button for textarea element loses focus?

The onchange JavaScript event is triggered when an element is changed and then loses focus. In the context of a textarea , this happens when the content of the textarea is modified and then the textarea loses focus because the user clicks away or presses the tab key.

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 } });


1 Answers

$("textarea").on("change keyup keydown paste cut copy", function(e) {

// do something

});

You can attach any event, you want to. Cut, Copy and Paste. I added all of them!

like image 188
Muhammad Talha Akbar Avatar answered Nov 15 '22 01:11

Muhammad Talha Akbar