Category: >> JavaScript >> Select text in a DIV, SPAN, or table cell Bookmark and Share

<< lastnext >>

Snippet Name: Select text in a DIV, SPAN, or table cell

Description: Great little snippet to select text in a static object like a DIV, SPAN, or table cell. Just pass in the container's ID and the text will be selected. Handy for use in selecting code blocks in message boards.

Also see:
» Copy a folder in Visual Basic
» Copy a file in VB
» Copying and Substituting Simultane...
» Copy Files By Date Modified
» Copy a remote file to your site
» JS ComboBox
» DHTML Combo Box Dropdown
» Hide/Show div
» Column copy with string replacement
» Smooth Div Show/Hide
» Copy one table to another
» Copy IFRAME contents
» Copy File From Server

Comment: (none)

Highlight Mode: JAVASCRIPT
Last Modified: December 08th, 2010

<head><script type="text/javascript">
	function fnSelect(objId) {
		if (document.selection) {
		var range = document.body.createTextRange();
		else if (window.getSelection) {
		var range = document.createRange();
	function fnDeSelect() {
		if (document.selection) document.selection.empty(); 
		else if (window.getSelection)
<div id="div1" style="background-color: #ffffdd;border: 1px solid #000;">
	<p>Sample text in div #1</p>
	<p>Sample text in div #1</p>
	<p>Sample text in div #1</p>
<div id="div2"  style="background-color: #ffffdd;border: 1px solid #000;">
	<p>Sample text in div #2</p>
	<p>Sample text in div #2</p>
	<p>Sample text in div #2</p>
<a href="javascript:fnSelect('div1');">Select text in div 1</a>
<a href="javascript:fnSelect('div2');">Select text in div 2</a>
<a href="javascript:fnDeSelect();">DeSelect text</a>
Add a comment 
SamMay 2nd, 2013 12:10pm
Excellent !

WayneDec 24th, 2014 8:39am
I am still developing an app, so no URL. You have finally enabled me to select text in a generated report in David Esperalta's HTML Compiler created by a series of eleven DIVs using code like this:

// Retrieve
document.getElementById("day1Result").innerHTML = "Day 1:- " +
sessionStorage.getItem("weekDay", "weekDay") ...etc.

After days of searching the Internet and JavaScript manuals and text books I have found what I have been looking for. You truly are a genius!
© | users online: 8