Simple Tooltip with jQuery (only text)
Tags: jQuery
Hello guys, I created a simple tooltip with jQuery, in this version display only the text. A tooltip is an interface component that appears when a user hovers over a control. They’re already present in most browsers; when you provide a title attribute for a link or an altattribute for an image, the browser will usually display it as a tooltip when the user hovers over that element. By storing the text using the data command, i can recover and replace the link "title" later.
HTML
The markup is really simple and flexible and adapts to many possible scenarios you might encounter. Must have a class "masterTooltip" which will launch the tooltip, and the tag "title" that will contain the text inside.
<a href="#" title="This will show up in the tooltip" class="masterTooltip">Your Text</a> <p title="Mouse over the heading above to view the tooltip." class="masterTooltip">Mouse over the heading text above to view it's tooltip.</p> <img src="image.jpg" class="masterTooltip" title="Tooltip on image" />
CSS
The styling is very simple, you can create anything with your creativity, this is just a default setting for your future applications.
.tooltip {
display:none;
position:absolute;
border:1px solid #333;
background-color:#161616;
border-radius:5px;
padding:10px;
color:#fff;
font-size:12px Arial;
}
jQuery
The first step is to call the jQuery file.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
Create a new script tag and insert the code.
<script type="text/javascript">
$(document).ready(function() {
// Tooltip only Text
$('.masterTooltip').hover(function(){
// Hover over code
var title = $(this).attr('title');
$(this).data('tipText', title).removeAttr('title');
$('<p class="tooltip"></p>')
.text(title)
.appendTo('body')
.fadeIn('slow');
}, function() {
// Hover out code
$(this).attr('title', $(this).data('tipText'));
$('.tooltip').remove();
}).mousemove(function(e) {
var mousex = e.pageX + 20; //Get X coordinates
var mousey = e.pageY + 10; //Get Y coordinates
$('.tooltip')
.css({ top: mousey, left: mousex })
});
});
</script>
The Logic
-
Grab the title
First we need to grab the title from the link, which will be the text we want our tooltip to display. We need to do this because we’ll be removing the title from the link, in order to prevent the browser from displaying its default tooltip that will conflict with our custom one. By storing the text using the data command, we can recover and replace the link title later. -
Hover over code
Create a new paragraph element with a class of tooltip, we use the text method to set the tooltip’s contents. Add new node to the page using appendTo and setting the animation with .fadeIn. -
Hover out code
It's simple, restoring the title attribute and removing the tooltip. -
Mouse move code
This is important, as we’ll need to access the X and Y coordinates of the event in order to position the tooltip. This way the tooltip will follow the mouse around.
Conclusion
We replaced a tooltip with our own, where we can manipulate both the style and animation. If anyone has any questions, concerns, or comments please feel free to let me know!
























31 comments…Add Comment
awesome blog, do you have twitter or facebook? i will bookmark this page thanks.
My hat is off to your asutte command over this topic-bravo!
Oh my goodness! It is like you read my mind! You seem to know so a lot about this, just like you wrote the book in it or some thing. I think that you could do with some pics to drive the content house a bit, besides that, this is helpful blog post. A outstanding read. I will definitely return again.
It’s a really good read I think, Must admit that you actually are one of the perfect bloggers I ever saw.Appreciate your sharing placing this interesting article.
I really enjoyed reading your web site and specifically this blog post. Is there any other way besides your RSS feed that we could get updates of future material? Thank you for your time.
Oh yes, follow me on Twitter
Perfect work you have done, this site is really cool with good information.
Well, the post is actually the freshest on this laudable topic. I concur with your conclusions and will thirstily look forward to your future updates.
Keep up the amazing work!! I love how you wrote this and I also like the colors here on this site. did you create this yourself or did you outsource it to a coder??
Thanks Johns, I created all by myself!
I loved reading this post I will be sure to tell my friends about this and link to it as well. Thanks
I look forward to seeing more great posts like these.
OH HAI…
Wonderful work! This is the type of info that should be shared around the internet. Shame on the search engines for not positioning this post higher! Come on over and visit my web site . Thanks =)…
superb….well designing..
Thank you! Works perfectly!
Rock on! So simple. Thank you.
Exactly what I needed. No difficult, hard to configure plugins with stupid mountains of CSS, just one simple CSS class and 20 lines of jQuery. Thanks!
I’ve changed the selector to [title], so now it works on all my titles throughout my site. Perfect!
Hi! I tried to use this code, but ít’s not working in Internet Explorer.
Could you explcain how to correct it?
Many thanks!
Drix
Strange, which version do you use? work in version 9, I have not tried the older versions.
Thanks for this. What if you want the tool tip to stay static based on the page… I want to keep the tooltip in the center of the page, no matter where it is accessed. Can this be done?
Do you know how to remove default tool tip in Internet explorer?
awr, as DUNCANC just said it, I can’t get rid of the default title msg in IE8/7 :-(
Any thoughts?
Delete Default Tooltip IE9 ?
I want to show image on mouse over. it is possible with that script???
Yes, it is possible!
Hey… Let me know how to show the image itself within the tooltip for the image mouse over…. like thumbnail hover show the larger image..
Very nice code snippet – thank you for sharing!
Hi,
Great code but I have a slight issue. In Internet Explorer 7-9 the code works perfectly but it also shows the title tag created by IE over the top of the tooltip. Any way of disabling that in IE so it just shows the modified title tag?
Sorry,
Just retested. That double effect is only an issue in IE9. 7 and 8 work perfectly. Also, and I am not sure if this would cause the issue, my images all have alt text but it is the title tag that IE9 is showing over the top of the tooltip.
Many thanks for any help.
Unfortunately shows both the normal tooltip and JQuery tooltip in IE7/8. Any way to get around this?
For remove the default tooltip see this post:
Disable tooltip in the browser with jQuery
Leave a response