Javascript select a div content

How to using a javascript command to select and deselect a div tag content?
As I know, we can use a select() funtion to select text field value:
document.getElementById(‘username’).select();
But the above example does not work for a div or span tag.

For the div or span tag, it is pretty simply, just few lines command will do.

Below is the example:

View Code JAVASCRIPT
1
2
3
4
5
if (document.selection){
  var range = document.body.createTextRange();
  range.moveToElementText(document.getElementById('ELEMENT_ID'));
  range.select();
}

But this example only work for IE(Internet Explorer).

Leave a Reply