javascript - Check selection inside or outside of a class -


i have following lines of script selected area of document.

    sel = window.getselection();     console.log(sel); 

i have results in console image below.

enter image description here

my question is,

is possible check selected text inside or outside of element specific class/id?

you can use anchornode (the node in selection begins) , focusnode (the node in selection ends) try determine whether or not selection in desired element.

assuming element you're interested direct parent of text being selected, can do:

var sel = document.getselection(); var startsintarget = sel.anchornode.parentelement.classlist.contains("target"); var endsintarget = sel.focusnode.parentelement.classlist.contains("target"); if(startsintarget && endsintarget) {      //selection within element class "target" } 

here's jsfiddle demonstrating idea


Comments

Popular posts from this blog

c - CUDA code not processing if block properly -

Android Capture Image From Camera -

Define custom list-style-type in CSS -