<?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>Lateral Code &#187; question</title>
	<atom:link href="http://www.lateralcode.com/tag/question/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lateralcode.com</link>
	<description>A Web Development Blog Focused on Code and Technology</description>
	<lastBuildDate>Thu, 26 Aug 2010 22:29:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Adding Tooltips to Option Tags</title>
		<link>http://www.lateralcode.com/adding-tooltips-to-option-tags/</link>
		<comments>http://www.lateralcode.com/adding-tooltips-to-option-tags/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 12:00:20 +0000</pubDate>
		<dc:creator>Karthik Viswanathan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://www.lateralcode.com/?p=642</guid>
		<description><![CDATA[A short while back, Abhilash SR commented on our poll with a question that he had: Is there any way to show the select box text as [a] tool tip when user moves the mouse down the list? In other words, Abhilash would like to know whether it&#8217;s possible to add tooltips to option tags. [...]]]></description>
			<content:encoded><![CDATA[<p><big>A short while back, Abhilash SR commented on our <a href="http://www.lateralcode.com/2009/02/poll-what-are-your-feelings-about-lateral-code/" title="Lateral Code Poll">poll</a> with a question that he had:</p>
<blockquote><p>Is there any way to show the select box text as [a] tool tip when user moves the mouse down the list?</p></blockquote>
<p></big></p>
<p>In other words, Abhilash would like to know whether it&#8217;s possible to add tooltips to <code>option</code> tags. Luckily, the answer is yes!</p>
<p><span id="more-642"></span></p>
<h2>Title Attribute</h2>
<p>There are many complicated ways of adding tooltips to the <code>option</code> tag, but the simplest one is accomplished by using the title attribute.</p>
<p>Why do I recommend using the title attribute?</p>
<ul>
<li>It is supported by Internet Explorer.</li>
<li>No JavaScript or CSS is required!</li>
<li>It only needs a short amount of code.</li>
</ul>
<p>Thus, adding a tooltip is as easy as:</p>
<pre><code>&lt;option title="Your tooltip here"&gt;Your text here&lt;/option&gt;</code></pre>
<h2>Automating the Process</h2>
<p>But, we at Lateral Code want to make this a bit more interesting. In Abhilash&#8217;s case, the text that is inside the <code>option</code> tag is exactly what should be used as the tooltip.</p>
<p>As many of us don&#8217;t like to waste time copy-pasting this text into the title attribute, why don&#8217;t we write a script to do it for us?</p>
<p>Let&#8217;s use jQuery to make this very straightforward. Our JavaScript will go through each <code>select</code> element. It will add a title attribute to every <code>option</code> tag inside the given element.</p>
<p>Our algorithm is as follows:</p>
<ol>
<li>Find the total number of <code>option</code> tags to loop through.</li>
<li>Loop through each tag.</li>
<li>Set the title attribute of the tag to it&#8217;s inner HTML.</li>
</ol>
<p>This yields:</p>
<pre><code>var total = $('select option').length;
var cur;
for ( var i = 0; i < total; i++ )
{
	cur = $('select option:eq(' + i + ')');
	cur.attr( 'title', cur.html() );
}</code></pre>
<p>Remember to import jQuery:<br />
</code></pre>
<pre><code>&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;</code></pre>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lateralcode.com/adding-tooltips-to-option-tags/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
