Get selected text on page with window.getSelection() method - Javascript Browser Object Model

Javascript examples for Browser Object Model:Window Load

Description

Get selected text on page with window.getSelection() method

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from  w ww .  j a va  2  s .c  o  m
window.find("I am");
var sel = window.getSelection(),
    frag = sel.rangeCount > 0 && sel.getRangeAt(0).cloneContents();
    console.log(frag && frag.querySelectorAll("b").length > 0);
}

      </script> 
   </head> 
   <body>
       Hello test, 
      <b>I</b>
       am in bold.  
   </body>
</html>

Related Tutorials