I am trying to get selected text of Iframe using Rangy using this function;
function getSelectedText() {
var iframe = document.getElementById("CustomeHTMLViewer");
var iframeWin = iframe.contentDocument ? iframe.contentDocument.defaultView : iframe.contentWindow;
var sel = rangy.getSelection(iframeWin);
return sel;
}
This code is not working; how can I get selected text of an Iframe?
Rangy has a convenience method for this: rangy.getIframeSelection()
. Example:
var iframe = document.getElementById("CustomeHTMLViewer");
var sel = rangy.getIframeSelection(iframe);
To get hold of the selected text, call toString()
on the selection:
var selectedText = sel.toString();
alert(selectedText);
Rangy allows an iframe element to be passed directly into rangy.getSelection()
.
Having said all that, there doesn't actually seem to be anything wrong with your original code.