Skip to content Skip to sidebar Skip to footer

Select All Or Highlight All Text In An Element

I've been searching high and low for something like this, but for some reason I'm having trouble making it work. I'm not sure what I'm missing or doing wrong. References found here

Solution 1:

Maybe, you could use window.getSelection() to get the global Selection objects, and then modify it? Example from developer.mozilla.org:

var strongs = document.getElementsByTagName("strong");
var s = window.getSelection();

if(s.rangeCount > 0) s.removeAllRanges();

for(var i = 0; i < strongs.length; i++) {
   varrange = document.createRange();
   range.selectNode(strongs[i]);
   s.addRange(range);
}

Solution 2:

Thanks guys, I really appreciate your help. I did some research on your responses, and had gotten a reply from the author of the script. I was pulling my hair out and kind of wanted to throw the puter out the window :P

Anyways, the author Satya stated that the select.js script needs to be at the END of the document rather in the header. It works great too!

<!DOCTYPE html><html><head><title>Bootstrap Banners Template</title><metaname="viewport"content="width=device-width, initial-scale=1.0"><!-- Bootstrap --><linkhref="css/bootstrap.css"rel="stylesheet"media="screen"><linkhref="css/bootstrap-theme.css"rel="stylesheet"media="screen"><linkhref="js/google-code-prettify/prettify.css"rel="stylesheet"><!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --><!-- WARNING: Respond.js doesn't work if you view the page via file:// --><!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]--></head><body><divclass="container"><divstyle="margin-top:50px;"></div><ulclass="nav nav-pills"><liclass="active"><ahref="#demo1"data-toggle="tab">Demo 1</a></li></ul><divclass="tab-content"><divstyle="margin-top:20px;"></div><divclass="tab-pane fade in active"id="demo1"><p></p><p>Copy and Paste Code:</p><preclass="prettyprint linenums lang-html">&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;head&gt;&lt;title&gt;Demo | Prettify.JS&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Hello, World!&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;</pre></div></div></div><divstyle="margin-top: 50px;"></div></div><scriptsrc="js/select2.js"></script><script>
  !function ($) {
    $(function(){
      window.prettyPrint && prettyPrint()   
    })
  }(window.jQuery)
</script></body></html>

Post a Comment for "Select All Or Highlight All Text In An Element"