Using Tippy without the tags

One question I get asked a lot is if it is possible to use Tippy directly on elements rather than through the Tippy short tag. The short answer is yes, it is possible, and here’s how.

This is a regular div, but I’ve added onmouseover and onmouseout calls to the div. The Tippy function is being called directly rather than using the shortcode to handle the call for me.

Here is how it looks:

<div tippyTitle="Tippy in a div" id="mydiv" onmouseover="Tippy.loadTipInfo('Here is an example of using Tippy with a div element.', 0, 0, 'mydiv', event);" onmouseout="Tippy.fadeTippyOut();">
div content
</div>

The most important parts are in onmouseover and onmouseout. The function Tippy.loadTipInfo() creates and displays the Tippy object, while Tippy.fadeTippyOut makes it go away.

Notice that in my example above, the div has a special attribute, tippyTitle. There are a few options for setting the tooltip title. With Tippy 3.6.4, options are limited: set the tippyTitle attribute, or let Tippy do its default behavior of grabbing the content of its tag, in this case, everything in the div. That would be a long title, so it’s far from ideal.

Tippy 3.7.0 adds two new options: manually specify, the title attribute, or pass the title to the Tippy.loadTipInfo() function. With Tippy 3.7.0, here are the parameters for loadTipInfo:

Tippy.loadTipInfo(text, width, height, id, event, title)

  • Text is whatever you want to display inside the body of the tooltip.
  • Width and height specify the size of the tooltip. Set 0 to use default values.
  • id passes along the id of the element containing the tooltip. In this case, we want the tooltip to attach to the div with id mydiv so we pass mydiv for the id parameter.
  • An event object must be passed in.
  • Finally, the new parameter is the title. When you write the Tippy code, instead of specifying the title or tippyTitle attribute on your div element, specify the title in the function call.

Here is another example:

Yay to Tippy in divs!

 

<div id="secondDiv" style="background-color: #ccc; padding: 2px;" onmouseover="Tippy.loadTipInfo('Display more tooltip info.', 0, 0, 'secondDiv', event, 'Second div example.');" onmouseout="Tippy.fadeTippyOut();">
Yay to Tippy in divs!
</div>

Note that when you manually write this out, you need to be careful of what you put in your tooltip text. Something like the following will not work:

<div id="badDiv" onmouseover="Tippy.loadTipInfo('I'll try to display an image: <img src="myimage.png" />', 0, 0, 'badDiv', event, 'This won't work.');" onmouseout="Tippy.fadeTippyOut();">

Single quotes and double quotes inside the text or title parameters will throw off the JavaScript. When you use the Tippy shortcode, the plugin will automatically run the text through the following PHP function:

htmlentities($tippyText, ENT_QUOTES, 'UTF-8');

If you want to manually add Tippy links to your layout, you may want to use this function to keep quotes from going where they don’t belong.

Hope that helps!

Trackback URL: http://croberts.me/2011/10/11/using-tippy-without-the-tags/trackback/
Short link: http://tcnr.me/fbkf7
4 comments
1.
Max
November 16, 2011 at 5:39 am
 

Sry! :) Just intresting in first question about links!

 
2.
Max
November 17, 2011 at 2:29 am
 

Chris, can you help me add links in my text without the tags? Sry, for my insistence!

 
3.
November 17, 2011 at 8:35 am
 

Max,

The post above should be enough to let you know how to make a Tippy link without using the tags. Is there something in the post that is particularly hard to follow? Perhaps I could help with a specific question.

 
4.
Max
November 18, 2011 at 3:23 am
 

Hi again! ^^ Ofc I understand how add Tippy link on website without tags (maybe I’ve described the problem incorrectly), my question is – how add links inside Tippy text (ex., how can I add BBC News inside the text).

I just want make Tippy without tags but as in your example “News Links” on page http://croberts.me/tippy-for-wordpress/

 

Add a Comment

Please provide the following information. Fields marked with * are required. Your email address will be kept private. The following tags may be used: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .
*
*