Hi,
After highlighting text, I would like to obtain the paragraph in which the selected text resides.
var select = window._content.document.getSelection();
Any pointers please?
Hi,
After highlighting text, I would like to obtain the paragraph in which the selected text resides.
var select = window._content.document.getSelection();
Any pointers please?
I found this useful example.
It seems that some browsers support window.getSelection() while others support document.getSelection(). The example handle all these cases.
This is actually rather hard to do because you have to consider six cases:
So firstly you have to decide how complete you want the solution to be. I'll only cover the simplest cases of (1) and (2).
function getSelectedParagraphText() {
var userSelection;
if (window.getSelection) {
selection = window.getSelection();
} else if (document.selection) {
selection = document.selection.createRange();
}
var parent = selection.anchorNode;
while (parent != null && parent.localName != "P") {
parent = parent.parentNode;
}
if (parent == null) {
return "";
} else {
return parent.innerText || parent.textContent;
}
}
Note: If you're after tags too replace textContent with innerHTML.
Edit: Better version put in, including better browser compatibility.
I have something useful... Unfortunately my script is written in italian language and is compatible only with Mozilla Firefox :(
...but I think it's very well constructed :)
You can find it here: http://sourceforge.net/projects/jsmatita.
P.S.: I'd like to translate it in english... if anybody is interested, contact me at: pincopalla00-at-gmail.com.
GoodBye :)
A new project is born from jsmatita: http://takenotes.sourceforge.net/ (it's in italian language)