Using jQuery's dialog I came across the following quirk (tested in FF3):
- User selects text
- In code, open up a jQuery dialog
- BUG: the text gets unselected
(text could be in a textarea or just an HTML on the page)
So, to me it seems like a funny (and annoying) bug or a quirk, but maybe there's a good explanation for that. And what interests me most, is how to preserve this text selection after opening the dialog?
Here's some code:
function getSelectedText() { var t; if (d.getSelection) t = d.getSelection(); else if(d.selection) t = d.selection.createRange(); if (t.text != undefined) t = t.text; if (!t || t=='') { var a = d.getElementsByTagName('textarea'); for (var i = 0; i < a.length; ++i) { if (a[i].selectionStart != undefined && a[i].selectionStart != a[i].selectionEnd) { t = a[i].value.substring(a[i].selectionStart, a[i].selectionEnd); break; } } } return t; } $("#dialog").dialog({ autoOpen: false, bgiframe: false, height: 60, width: 80, modal: false, show: 'highlight', title: 'wc'}); alert(getSelectedText()); // Text is here $("#dialog").dialog("open"); alert(getSelectedText()); // Text is not selected here :( damn! Thanks!