Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i catch the firefox spellcheck correction event?

I have a textarea. After writing some misspelled text and using rightclick -> correction the word gets replaced with a correctly spelled word. Now, my problem here is that i need to exectue some javascript code when the correction gets done.

How can i catch the firefox spellcheck correction event? If there is a only a solution using a firefox Add-On i would be happy too to know that one.

like image 577
Thariama Avatar asked Jan 31 '12 10:01

Thariama


2 Answers

Mozilla fires oninput in this case, didn't test in others, but should work everywhere.

Interestingly enough, FF seems to fire two input events when using spelling correction: it deletes the word first, and then inserts the new one:

> value=[holy coww]
(right click and choose "cow")
> value=[holy ]
> value=[holy cow]

http://jsfiddle.net/7ssYq/

like image 146
georg Avatar answered Nov 07 '22 02:11

georg


I was originally going to suggest the oninput event, like thg435's answer, but I thought I'd fish for more details in the comments first. If you don't need to differentiate between spell checker corrections and other types of input (keyboard, paste, drag and drop, etc), then oninput would do the job just fine.

If you do want to differentiate between those types of input, then I'm afraid there's no event that fires specifically for spell checker corrections. However, there are events for most other types of input, so you could at least narrow down the likelihood of your input event being a correction if you check for other types of event first. Consider the following:

(function () {
    var el = document.getElementById("MyInput"),
        ignore = false;

    el.oninput = function (e) {
        // ignore the events that we don't need to capture
        if (ignore) {
            ignore = false;
            return true;
        }

        // Your code here
    }

    // IIRC, you need the following line for the `ondrop` event to fire
    el.ondragover = function () { return false; }

    // Ignore paste, drop and keypress operations
    el.onpaste = el.ondrop = el.onkeypress = setIgnore;

    function setIgnore (e) {
        ignore = true; 
    }
})();

This isn't a perfect solution, however. For instance, the event will still fire for Undo/Redo actions (and, perhaps some other actions) that aren't initiated by the keyboard.

like image 21
Andy E Avatar answered Nov 07 '22 03:11

Andy E