Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWeb Content Preparation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Click to Select

The content within HTML container elements (like div, pre, p, and td) can be made selectable with a click or a tap. To do so, JavaScript is required.

When content is selected, the website user can do what they want with it. Generally, they would copy the selected content for pasting somewhere else. (Selecting content does not automatically copy the content.)

Keep in mind that this selection feature may not work in most email readers, as they typically do not support JavaScript.

Here is an example of a container element, specifically a paragraph (p tag).

This paragraph is an example of content that may be selected when tapped. When you tap the text, JavaScript causes the text to be selected. When you tap anywhere else, the browser will automatically deselect the text.

To enable an HTML container element to have its content selected, such as a p tag like the above example, first put this JavaScript anywhere in the web page.

<script type="text/javascript">
function containerSelect(id) {
if(document.selection) {
   var range = document.body.createTextRange();
   range.moveToElementText(id);
   range.select();
   }
else if(window.getSelection) {
   var range = document.createRange();
   range.selectNode(id);
   window.getSelection().addRange(range);
   }
}
</script>

Then, put this attribute into the p tag (or other HTML container element). Once the above JavaScript is in the web page source code, this attribute may be put into the tag of as many HTML container elements as you want.

onclick="containerSelect(this)"

For reference, here is the complete source code for the example paragraph.

<p onclick="containerSelect(this)" style="border:3px dotted black; padding:1em; border-radius:1em;">
This paragraph is an example of content that may be selected when tapped. When you tap the text, JavaScript causes the text to be selected. When you tap anywhere else, the browser will automatically deselect the text.
</p>

It is fairly simple to implement tap-to-select functionality.

  1. Ensure the JavaScript is on the web page.

  2. Insert the onclick="containerSelect(this)" attribute into every tag that shall have click-selectable text.

For additional examples of use, see the copiable code in most Possibilities articles. The code is generally in click-to-select pre tags.

(This content first appeared in Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC