<?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>A Virtual Insight - Richard Turner &#187; jQuery</title>
	<atom:link href="http://www.richardturner.com/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardturner.com</link>
	<description></description>
	<lastBuildDate>Fri, 23 Jul 2010 18:36:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>jQuery Images in a Select Menu Dropdown</title>
		<link>http://www.richardturner.com/2010/01/jquery-images-in-a-select-menu-dropdown/</link>
		<comments>http://www.richardturner.com/2010/01/jquery-images-in-a-select-menu-dropdown/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 17:02:47 +0000</pubDate>
		<dc:creator>Richard Turner</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[FireFox]]></category>
		<category><![CDATA[Internet Explorer]]></category>

		<guid isPermaLink="false">http://www.richardturner.com/?p=122</guid>
		<description><![CDATA[A client had a dropdown that listed fonts, they wanted each option to be an image preview of the font in the select menu, now since some of these were not web-safe font&#8217;s I could not use CSS &#8216;font-family&#8217;. I originally wrote about 3 lines of code append an img tag to the option tag [...]]]></description>
			<content:encoded><![CDATA[<p>A client had a dropdown that listed fonts, they wanted each option to be an image preview of the font in the select menu, now since some of these were not web-safe font&#8217;s I could not use CSS &#8216;font-family&#8217;. I originally wrote about 3 lines of code append an img tag to the option tag which worked great in FireFox but no others.</p>
<p>I then scratched my head for a while and browsed the web for an example, I found one, ajaxload.info, so i extracted their JavaScript, rewrote it into jQuery, customised it to suit my needs. All done, Firefox &#8211; check, Chrome &#8211; check, IE &#8211; no. What a surprise, okay ajaxload.info in IE &#8211; load. Hmm theres doesn&#8217;t work either.</p>
<p>So about 6 hours after starting this seemingly easy task, i finally came up with a working version for FireFox, Chrome, IE, Safari etc. And here it is to save someone else the headache should they ever need it.</p>
<pre class="chili"><code class="=html"""""">&lt; !DOCTYPE HTML PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
	&lt;style type=&quot;text/css&quot;&gt;
	select {
		z-index: 1;
	}

	.mask {
		width: 150px;
		z-index: 100;
		background: url(transparentGif.gif);
		position: absolute;
		padding: 2px;
	}

	div.typelist {
		background: #FFFFFF;
		border: 1px solid #868686;
		overflow-x: hidden;
		overflow-y: auto;
		display: none;
		position: absolute;
		width: 148px;
		height: 150px;
	}

		div.typelist a {
			display: block;
			padding: 10px 20px;
			width: 100%;
			border: 0px;
			outline: 0px;
		}

		div.typelist a img {
			border: 0px;
		}
	&lt;/style&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.microsoft.com/ajax/jQuery/jquery-1.3.2.js&quot;&gt;&lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot;&gt;
	var img_dir = &#039;&#039;; //with trailing slash

	function RT_ShowImageMenu(id) {
		$(&#039;div.typelist&#039;).css(&#039;display&#039;, &#039;none&#039;); //close all

		$(&#039;#typelist-&#039; + id).css(&#039;display&#039;, &#039;block&#039;);
		document.onclick = function() {
			document.onclick = function() {
				$(&#039;#typelist-&#039; + id).css(&#039;display&#039;, &#039;none&#039;);
				document.onclick = null;
			}
		}
	}

	function RT_MakeImageMenu() {
		$(&#039;select.image-select&#039;).each(function(){
			var mask_id = &#039;mask-&#039; + $(this).attr(&#039;id&#039;);
			var mask = $(&#039;&lt;div id=&quot;&#039; + mask_id + &#039;&quot; class=&quot;mask&quot; onclick=&quot;RT_ShowImageMenu(\&#039;&#039; + $(this).attr(&#039;id&#039;) + &#039;\&#039;); &quot;&gt;&lt;/div&gt;&#039;);
			$(mask).insertBefore($(this));

			$(&#039;#&#039; + mask_id).css( { height: $(this).height(), width: $(this).width() } );

			typelist_id = &#039;typelist-&#039; + $(this).attr(&#039;id&#039;);
			typelist = $(&#039;&lt;div id=&quot;&#039; + typelist_id + &#039;&quot; class=&quot;typelist&quot;&gt;&lt;/div&gt;&#039;);
			$(typelist).insertAfter($(this));

			$(this).find(&#039;option&#039;).each(function(){
				$(&#039;#&#039; + typelist_id).append(&#039;&lt;a href=&quot;javascript:RT_SelectOption(\&#039;&#039; + $(this).parent().attr(&#039;id&#039;) + &#039;\&#039;, \&#039;&#039; + $(this).val() + &#039;\&#039;)&quot; title=&quot;&#039; + $(this).html() + &#039;&quot;&gt;&lt;img src=&quot;&#039; + img_dir + $(this).val() + &#039;.png&quot; alt=&quot;&#039; + $(this).html() + &#039;&quot; /&gt;&lt;/a&gt;&#039;);
			});
		});
	}

	function RT_SelectOption(element, id) {
		$(&#039;#&#039; + element).val(id);
		$(&#039;#typelist-&#039; + element).css(&#039;display&#039;, &#039;none&#039;);
	}

	$(document).ready(function() {
		RT_MakeImageMenu();
	});
	&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
      &lt;select id=&quot;font&quot; name=&quot;font&quot; class=&quot;image-select&quot;&gt;
        &lt;option value=&quot;1&quot;&gt;Arial&lt;/option&gt;
        &lt;option value=&quot;2&quot;&gt;Verdana&lt;/option&gt;
        &lt;option value=&quot;3&quot;&gt;Courier&lt;/option&gt;
        &lt;option value=&quot;4&quot;&gt;Tahoma&lt;/option&gt;
      &lt;/select&gt;

      &lt;br /&gt;&lt;br /&gt;

      &lt;select id=&quot;font2&quot; name=&quot;font2&quot; class=&quot;image-select&quot;&gt;
        &lt;option value=&quot;1&quot;&gt;Arial&lt;/option&gt;
        &lt;option value=&quot;2&quot;&gt;Verdana&lt;/option&gt;
        &lt;option value=&quot;3&quot;&gt;Courier&lt;/option&gt;
        &lt;option value=&quot;4&quot;&gt;Tahoma&lt;/option&gt;
      &lt;/select&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p><a href="http://www.richardturner.com/jquery/image-select/index.html" target="_blank">View Example</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardturner.com/2010/01/jquery-images-in-a-select-menu-dropdown/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
