javascript - Selected text inside div -


i want text inside <div> highlighten (selected copy) when page loads.

<div id='sample_div'>this sample text</div> 

what tried:

$(document).ready(function(){  $('#sample_div').select();  }); 

so if assume correctly, want text inside particular div highlighted..

function selecttext(element) {     var doc = document         , text = doc.getelementbyid(element)         , range, selection     ;         if (doc.body.createtextrange) { //ms         range = doc.body.createtextrange();         range.movetoelementtext(text);         range.select();     } else if (window.getselection) { //all others         selection = window.getselection();                 range = doc.createrange();         range.selectnodecontents(text);         selection.removeallranges();         selection.addrange(range);     } } 

working jsfiddle

source


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -