Click here to Skip to main content
14,297,581 members

Substitute Click for Hover on Touch Devices

Rate this:
0.00 (No votes)
Please Sign up or sign in to vote.
0.00 (No votes)
24 Mar 2011GPL3
Alternative to hover events when we design our user experience



Hovering doesn’t work out so well on devices with a touch interface.


A substantial portion, if not the majority of web browsing will be done with touch devices.


We need to create alternatives to hover events when we design our user experience.

A Solution

In this tutorial, I will show you a brief example of just such an alternative. In the example, we have an <abbr> with a tooltip. I use the <abbr> tag for two important reasons. First, in modern browsers, <abbr> tags will produce a browser tooltip by default anyway so we will have graceful degradation. Second, we leverage this by putting the text of the tooltip in the <abbr>‘s title attribute and this allows us to use jQuery to grab this text and use it for the content of our custom tooltip.

The Code


Just a simple document with an abbreviation that has a tooltip.

<!DOCTYPE html>
    <link rel="stylesheet" href="tooltip.css" />
    <p>For a tooltip hover or 
       click <abbr title="This text will show up in your tooltip!">here</abbr>.</p>
    <script type='text/javascript' 

    <script type='text/javascript' src='touchHover.js'></script>


Styles for the abbreviation and the tooltip.

        border-bottom: 1px dotted #666;
        cursor: help;

	border: 1px solid #aaaaca;
	font-size: smaller;
	width: 160px;
	box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.1);
	-moz-box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.1);
	-webkit-box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.1);

The JS

Here’s what the example is all about. We get rid of the browsers tooltip and make our own. Then, we make a clickable tooltip that fades out after a short delay.

 * touchHover.js
 * Create tooltips on mouseover or on click (for supporting touch interfaces).
 * @author C. Scott Asbach

$(document).ready(function() {	

	 * store the value of and then remove the title attributes from the
	 * abbreviations (thus removing the default tooltip functionality of
     * the abbreviations)


	 * when abbreviations are mouseover-ed show a tooltip with the data from the title attribute
	$('abbr').mouseover(function() {		

		// first remove all existing abbreviation tooltips

		// create the tooltip
		$(this).after('<span class="tooltip">' + $(this).data('title') + '</span>');

		// position the tooltip 4 pixels above and 4 pixels to the left of the abbreviation
		var left = $(this).position().left + $(this).width() + 4;
		var top = $(this).position().top - 4;

	 * when abbreviations are clicked trigger their mouseover event then fade the tooltip
	 * (this is friendly to touch interfaces)


		// after a slight 2 second fade, fade out the tooltip for 1 second
		$(this).next().animate({opacity: 0.9},{duration: 2000, complete: function(){

	 * Remove the tooltip on abbreviation mouseout


Happy Clicking!

There you have it. While this is a basic example, with a little imagination, you could apply similar techniques to all of your essential interactions that are currently dependent on hovers.

Got the Gist?


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

C. Scott Asbach
Web Developer piddmedia
United States United States
No Biography provided

Comments and Discussions

-- There are no messages in this forum --
Technical Blog
Posted 24 Mar 2011

Tagged as


2 bookmarked