Home » jQuery

jQuery: A simple Tooltip

8 February 2010 1,519 views Popularity: 3% Share/Bookmark

Here, I will show you how to show tooltip with jQuery. It’s very simple and easy.

I have a href link. I will show you, how to show tooltip when you hover your mouse over the link. The title of the href link will be shown as tooltip.

View Demo || Download Code

In the demo, I have kept two links. One without tooltip and the other with tooltip.

<h2>Tooltip</h2>
<a href="#" title="Save Earth and Be Happy!">Save the planet Earth</a><br/> (only with title but No tooltip)<br/><br/>
<a class="tooltip" href="#" title="Save Earth and Be Happy!">Save the planet Earth</a><br/> (with tooltip)<br/><br/>
<p id="tip"></p>

Tooltip is shown when the link is hovered. The title of the href link is shown as tooltip. The tooltip position is set with e.PageY and e.PageX. The tooltip is shown slowly with fadeIn(“slow”). And the tooltip is made hidden with fadeOut(“fast”) when we don’t hover over the link or move the cursor away from the link.

$(function(){
	var tip = $("#tip");

	$("a.tooltip").hover(function(e){

		// assign the <a> title to tip
		tip.text($(this).attr("title"));

		// empty <a> title
		$(this).attr("title", "");

		// set the position of tooltip and then display it
		tip.css("top",(e.pageY+5)+"px")
		   .css("left",(e.pageX+5)+"px")
		   .fadeIn("slow");

	}, function(){

		// hide tooltip
		$("#tip").fadeOut("fast");

		// set the <a> title with tootip content
		$(this).attr("title", tip.html());
	});
});

View Demo || Download Code

From Mukesh Chapagain's Blog, post jQuery: A simple Tooltip

Related posts:

  1. jQuery: Preview Image with Tooltip Effect
  2. Simple and easy jQuery tabs with AJAX and PHP
  3. jQuery: A simple Slideshow
  4. jQuery: How to replace string, div content and image src?
  5. jQuery: Print array and object
  6. PHP: Simple and easy way to format URL string
  7. jQuery: Preview Image with Zoom Effect
  8. jQuery: Cool Bouncing Navigation with easing plugin
  9. jQuery: Set time interval between events with queue function
  10. jQuery: Grey out background and preview image as popup