Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

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!

Tap to Select

Allowing site visitors to select content with a tap can be a sign of caring. That's especially true for content they are likely to want to copy, like source code to reuse or a list of requirements for something.

I'll describe how to implement the functionality.

The first step to implement the functionality is to put this JavaScript somewhere on the web page. No customization is required. The JavaScript can be anywhere that it's convenient. Placing it near the bottom of the page, above the cancel </body> tag is good.

<script type="text/javascript">
function SelectContent(d)
{
   if(document.selection)
   {
      document.selection.empty(); // First, unselect any already selected.
      var range = document.body.createTextRange();
      range.moveToElementText(d);
      range.select();
   }
   else if(window.getSelection)
   {
      window.getSelection().removeAllRanges(); // First, unselect any already selected.
      var range = document.createRange();
      range.selectNode(d);
      window.getSelection().addRange(range);
   }
}
</script>

With the JavaScript in place, you are now ready to provide tap-to-select ease to your site visitors.

The content within any HTML content container can be selected with a tap — except form fields, which use a different mechanism for one‑tap selection. There are three ways: Tapping the content within the container, tapping a link to select, or tapping a button to select.

Each of the following examples contains the example and then the source code.

Tap Content Within Container

The div is not required to have an id value to use this method.

Tap on the content of this div to select the content.
<div onclick="SelectContent(this)" style="max-width:2in; border:1px dashed black; padding:.5em;">
Tap on the content of this div to select the content.
</div>

Tap a Link

The div must have an id value to use this method. To implement, the link code can be anywhere on the web page.

Tap me
<a href="javascript:SelectContent(document.getElementById('a-link-demo'))">Tap me</a>
<div id="a-link-demo" style="max-width:2in; border:1px dashed black; padding:.5em;">
Tap the "Tap me" link (above this div) to select this content.
</div>

Tap a Button

The div must have an id value to use this method. To implement, the button code can be anywhere on the web page.

Tap the "Tap me" button (above this div) to select this content.
<input type="button" onclick="SelectContent(document.getElementById('a-button-demo'))" value="Tap me" style="width:fit-content;">
<div id="a-button-demo" style="max-width:2in; border:1px dashed black; padding:.5em;">
Tap the "Tap me" button (above this div) to select this content.
</div>

Making it easy for your site visitors to select content they probably will want to copy is a gesture of friendliness.

(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-2025 Will Bontrager Software LLC