<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Feet Have Been &#187; tooltip</title>
	<atom:link href="http://wheremy.feethavebeen.com/tag/tooltip/feed/" rel="self" type="application/rss+xml" />
	<link>http://wheremy.feethavebeen.com</link>
	<description>Sometimes, you just go where your feet take you</description>
	<lastBuildDate>Tue, 24 Jan 2012 17:39:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Tooltips</title>
		<link>http://wheremy.feethavebeen.com/2009/07/tooltips/</link>
		<comments>http://wheremy.feethavebeen.com/2009/07/tooltips/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 19:20:17 +0000</pubDate>
		<dc:creator>xangelo</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[tooltip]]></category>

		<guid isPermaLink="false">http://wheremy.feethavebeen.com/?p=132</guid>
		<description><![CDATA[You&#8217;ve probably seen them multiple times while never really wondering how they&#8217;re done. Tooltip are one of the handiest features developers have to let people know what a particular object does. Tooltips can be anything from descriptions to definitions, or even provide additional information about an object. Most frequently, you can see tooltips in games. [...]]]></description>
			<content:encoded><![CDATA[<p>You&#8217;ve probably seen them multiple times while never really wondering how they&#8217;re done. Tooltip are one of the handiest features developers have to let people know what a particular object does. Tooltips can be anything from descriptions to definitions, or even provide additional information about an object. Most frequently, you can see tooltips in games. Since you only have a limited amount of screen-room in games, developers need to think about how to show you most of the game, while minimizing the amount of helpful text that you see. In first person shooters this is often very easy, but when you move to games like Starcraft or Star Wars: Galactic Battlegrounds it becomes too hard to display enough information without layering modal windows and tooltips.</p>
<p>The same can be said about web pages. Rather than bombard a user with lists and paragraphs of text, maybe things can be more easily managed. The easiest example of this, would be definitions. Imagine coming across a word that you don&#8217;t understand. Wouldn&#8217;t it be easier if the developer just added a little feature where you could hover over a word and it would show a little popup telling you the definition?The following screenshots show a few examples of what I&#8217;m talking about:</p>
<p><img class="aligncenter size-full wp-image-133" title="jx01" src="http://wheremy.feethavebeen.com/wp-content/uploads/2009/07/jx01.jpg" alt="jx01" width="355" height="67" /><img class="aligncenter size-full wp-image-134" title="jx02" src="http://wheremy.feethavebeen.com/wp-content/uploads/2009/07/jx02.jpg" alt="jx02" width="445" height="102" />Note that with the second image, the tooltip is clearly hovering on top of some text, as well, the tooltip graphic is a little more detailed than the first, which is a simple span.</p>
<p>To achieve this rather &#8220;complex&#8221; effect, you can simple have attach onmouseover/onmouseout events to the trigger text and a simple function which accepts the ID of the target as an attribute. The code for this would simply be:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> tooltip<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>
 document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">else</span>
 document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;</span>a onmouseover<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;tooltip('target')&quot;</span> onmouseout<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;tooltip('target')&quot;</span><span style="color: #339933;">&gt;</span>Trigger<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;target&quot;</span> style<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;display: none; &quot;</span><span style="color: #339933;">&gt;</span>Some Text<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

<p>While it seems simple enough, once you start monitoring a lot more events, things get a lot more confusing. The easiest way to handle something like this is to just create a function that will accept two values. The first will be the trigger and the second will be the target. A very simple concept, but one that takes a little more work than you would think.</p>
<p>For starters, there is no way for you to simply add &#8220;element.onmouseover&#8221; in javascript. Instead you have to use &#8220;element.addEventListener&#8221; which accepts 3 arguments. </p>
<p>Before we get to that stage, lets first set up our main function. Since javascript accepts $ as a string, lets use that, along with some other characters, as our function name. The reason being that it is almost guaranteed that you will never come across another function by that name unless you start using frameworks.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> $_jx<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A good way to think about programming is to think about use before implementing a structure that you may not like. For example, I want to be able to write</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$_jx.<span style="color: #660066;">tooltip</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'trigger'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'target'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and have it automatically assign the tooltip functionality to the trigger. In order to achieve this, we need to start working with functions within functions (and yes, even a fourth tier of that). Since tooltip is within $_jx, we can define it like this</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> $_jx<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">tooltip</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>trigger<span style="color: #339933;">,</span>target<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that we are using an anonymous function and using tooltip as an alias. A little strange, but it ensures easy use down the line. To attach the tooltip event to our trigger, we need to make use of &#8220;addEventListener&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> $_jx<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">tooltip</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>trigger<span style="color: #339933;">,</span>target<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> trigger <span style="color: #339933;">=</span> trigger<span style="color: #339933;">;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> target <span style="color: #339933;">=</span> target<span style="color: #339933;">;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> offset<span style="color: #339933;">;</span> 
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>trigger<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">,</span>whattodo<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>First we&#8217;re going to create some local variables that will be visible to this.tooltip and all its children. Since addEventListener does not let us pass arguments to functions defined within the &#8220;whattodo&#8221; section, we need some way of accessing them without over-writing any other variables. </p>
<p>The whattodo section will be a little confusing. What we will actually be doing is using yet another anonymous function. I&#8217;ll show you the function code below and then I&#8217;ll show you what our entire tooltip function will look like.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The entire function now looks like this</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> $_jx<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">tooltip</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>trigger<span style="color: #339933;">,</span>target<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> trigger <span style="color: #339933;">=</span> trigger<span style="color: #339933;">;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> target <span style="color: #339933;">=</span> target<span style="color: #339933;">;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> offset<span style="color: #339933;">;</span> 
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>trigger<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It isn&#8217;t pretty, but it is effective. That will cause our &#8220;target&#8221; to by displayed when you hover over the &#8220;trigger&#8221;. However, if you move your mouse off the trigger, the target stays displayed. That is easily fixed and is essentially a copy/paste of the addEventListener section that we have right now. We will only change mouseover to mouseout and the value for document.getElementById(target).style.display to none.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> $_jx<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">tooltip</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>trigger<span style="color: #339933;">,</span>target<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> trigger <span style="color: #339933;">=</span> trigger<span style="color: #339933;">;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> target <span style="color: #339933;">=</span> target<span style="color: #339933;">;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> offset<span style="color: #339933;">;</span> 
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>trigger<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>trigger<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseout,function (){
        document.getElementById(target).style.display = '</span>none<span style="color: #3366CC;">'; 
    },false); 
  }
}</span></pre></div></div>

<p>And there you go. To use this all you will need to do is the following.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">1. Include the script after &lt;/body&gt; Since the tooltips don't need to be seen until AFTER the page is loaded, don't waste precious load time loading up the script. 
&nbsp;
&lt;script src=&quot;filename.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
$_jx <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> $_jx<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
$_jx.<span style="color: #660066;">tooltip</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'trigger'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'target'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>You just need to make sure that style=&#8221;display: none&#8221; is present in the target&#8217;s tag. Other than that, no modifications need to be made to your existing code to add tooltips. </p>
]]></content:encoded>
			<wfw:commentRss>http://wheremy.feethavebeen.com/2009/07/tooltips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

