lot of other d...">

How to find parent div, selection range belongs

My code is in the following format

<div id="viewer">
   <div id="pageContainer1">
       lot of other divs are inside this 
  </div>
   <div id="pageContainer2">
       lot of other divs are inside this 
  </div>
   <div id="pageContainer2">
       lot of other divs are inside this 
  </div>
 </div>

I want that whenever a text selection appears on the page, I want to get the div "pageContiner" in which it is located.

+3
source share
1 answer

do you mean this

function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

$(document).ready(function (){
    $('#viewer div *').mouseup(function (e){
      alert($(this).closest('[id^="pageContainer"]').attr('id'));
    })
});
+2
source

All Articles