- Jesse Ruderman: Finding the textarea selection
-
A desperate web developer emailed me asking how make a bookmarklet that does something with the selected text, where the selected text is usually in a textarea. He had tried using window.getSelection.toString(), but that doesn't work, because window.getSelection() is implemented in terms of DOM Ranges and it doesn't make much sense to have a DOM Range inside a textarea. Here are some of the methods I tried: Determine focus using by tracking with onfocus or onblur events. Works for web pages, but doesn't work for bookmarklets. Determine focus using window.getSelection().getRangeAt(...) and commonAncestorContainer. Doesn't work (bug 336936). Determine focus using a XUL document in an iframe and commandDispatcher.focusedElement. Ugly Firefox-only hack, but it works. Determine focus using WhatWG's document.currentFocus. Only a working draft, not implemented in Firefox yet. Use selectionStart / selectionEnd in all textareas. This can return nonempty selections from multiple textareas. Am I missing a sane solution that works in current versions of Firefox?
Sun, 07 May 2006 04:10:15 +0200 - more Planet Mozilla articles
-
- no related articles found at Planet Mozilla