1

I want to access all the nodes that are in selected area of HTML.

var sel = window.getSelection();
var range = sel.getRangeAt(0);

How to proceed after that?

dev_android
  • 8,698
  • 22
  • 91
  • 148
  • I was curious how you could have such a high reputation score with such a poor question, then noticed that you ask lots of questions then answer them as well. Makes me remember what is wrong with this site! – Lee Willis May 01 '15 at 11:12
  • I am from different domain, for some work, I have to work with Javascript for Android WebView. – dev_android May 01 '15 at 11:23

1 Answers1

0

You could do:

var sel = window.getSelection();
sel.toString()

To get the content of the selection

Check this other post that uses the onMousUp event to see on what element the selection ends: How can I get the DOM element which contains the current selection?

function getSelectionBoundaryElement(isStart) {
    var range, sel, container;
    if (document.selection) {
        range = document.selection.createRange();
        range.collapse(isStart);
        return range.parentElement();
    } else {
        sel = window.getSelection();
        if (sel.getRangeAt) {
            if (sel.rangeCount > 0) {
                range = sel.getRangeAt(0);
            }
        } else {
            // Old WebKit
            range = document.createRange();
            range.setStart(sel.anchorNode, sel.anchorOffset);
            range.setEnd(sel.focusNode, sel.focusOffset);

            // Handle the case when the selection was selected backwards (from the end to the start in the document)
            if (range.collapsed !== sel.isCollapsed) {
                range.setStart(sel.focusNode, sel.focusOffset);
                range.setEnd(sel.anchorNode, sel.anchorOffset);
            }
       }

        if (range) {
           container = range[isStart ? "startContainer" : "endContainer"];

           // Check if the container is a text node and return its parent if so
           return container.nodeType === 3 ? container.parentNode : container;
        }   
    }
}

See an example at: http://jsfiddle.net/pmrotule/dmjsnghw/

Community
  • 1
  • 1
GraafM
  • 91
  • 7