title: javascript to select text from web page
body: <p>I'm trying to write a bookmarklet that grabs any selected text on a web page and sends it to my website.  It should (hopefully) work in Chrome, FFX, Safari, and IE.  I did a search and found a function, but it doesn't appear to work.  Here is the code:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  &lt;div onClick=getSelText()&gt;Click&lt;/div&gt;
  &lt;div&gt;please select me&lt;/div&gt;
  &lt;/body&gt;
  &lt;script language=javascript&gt;
  function getSelText(){
      var txt = 'nothing';
      if (window.getSelection){
          txt = "1" + window.getSelection();
      } else if (document.getSelection) {
          txt = "2" + document.getSelection();
      } else if (document.selection) {
          txt = "3" + document.selection.createRange().text;
      } else return;
  
      alert("selected text = " + txt);
  }
  &lt;/script&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>when I select the text in the div "please select me" and hit the click div, I just get "selected text = 1"</p>
  
  <p>thanks</p>