<?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>Cool Javascripts &#187; dynamic icons</title>
	<atom:link href="http://cool-javascripts.com/tag/dynamic-icons/feed" rel="self" type="application/rss+xml" />
	<link>http://cool-javascripts.com</link>
	<description>The best javascripts for your site</description>
	<lastBuildDate>Sun, 12 Jul 2009 07:40:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Add icons to your links automatically using jQuery &amp; CSS</title>
		<link>http://cool-javascripts.com/jquery/add-icons-to-your-links-automatically-using-jquery-css.html</link>
		<comments>http://cool-javascripts.com/jquery/add-icons-to-your-links-automatically-using-jquery-css.html#comments</comments>
		<pubDate>Mon, 12 Jan 2009 09:54:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[dynamic icons]]></category>
		<category><![CDATA[link dressup]]></category>
		<category><![CDATA[link styles]]></category>

		<guid isPermaLink="false">http://cool-javascripts.com/jquery/add-icons-to-your-links-automatically-using-jquery-css.html</guid>
		<description><![CDATA[You want to show a particular icon to a particular type of link. For eg: a pdf icon to all pdf file links, a text document icon to all text document links, zip icon to all links that are linked to zip files etc.

Why jQuery?

Yes, it is not that difficult with css. Just declare some [...]


Related posts:<ol><li><a href='http://cool-javascripts.com/jquery/vertical-alignment-of-contents-inside-an-element-using-jquery.html' rel='bookmark' title='Permanent Link: Vertical alignment of contents inside an element using jQuery'>Vertical alignment of contents inside an element using jQuery</a> <small>One of the old problems! Vertical alignment of contents inside...</small></li><li><a href='http://cool-javascripts.com/jquery/building-a-reusabe-font-size-controller-interface-using-jquery.html' rel='bookmark' title='Permanent Link: Building a reusabe font size controller interface using jQuery'>Building a reusabe font size controller interface using jQuery</a> <small>Most of the web2.0 design allows the user to change...</small></li><li><a href='http://cool-javascripts.com/effects/make-curved-corners-with-jquery-corner-plugin.html' rel='bookmark' title='Permanent Link: Make curved corners with jQuery Corner plugin'>Make curved corners with jQuery Corner plugin</a> <small>If you want a curved edge for your element, the...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>You want to show a particular icon to a particular type of link. For eg: a pdf icon to all pdf file links, a text document icon to all text document links, zip icon to all links that are linked to zip files etc.</p>
<p><img src="http://cool-javascripts.com/wp-content/uploads/2009/01/dynamic-icons.jpg" /></p>
<p><b>Why jQuery?<br />
</b><br />
Yes, it is not that difficult with css. Just declare some classes and give background images then assign that class to the links as required. Then why we need jQuery. The answer is, for simplicity. Who ever entering the content should be cautious to add the right classes for the right links. Also adding classes are difficult in a blog platform &#8211; you will need to switch to HTML view instead of the editor view.</p>
<p>Using jQuery we&#8217;ll make sure that the right icon is showing for a particular link. <span id="more-99"></span></p>
<p><b>Step 1 &#8211; Find the right icons</b></p>
<p>Getting a free icon describing your file type is easy. You might need to search for free icon sets, or better search in any of the three following icon search engines. For eg: search for the term &#8220;pdf&#8221; and you will see a lot of images in different dimensions, select the one that suits your need. Just make sure you&#8217;re not using a copyrighted icon <img src='http://cool-javascripts.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
<ol>
<li><a rel="shadowbox" href="http://www.iconlook.com" rel="shadowbox">Icon Look &#8211; http://www.iconlook.com</a></li>
<li><a rel="shadowbox" href="http://www.iconlet.com">Icon Let &#8211; http://www.iconlet.com</a></li>
<li><a href="http://www.iconfinder.net">Icon Finder &#8211; http:www.iconfinder.net</a></li>
</ol>
<p>Here I am using five icons &#8211; One for pdf files, one for txt documents, one for zip files, one for email links and one for external links. You may add any number of icons for any file type as I am going to describe in the next sections.</p>
<p class="note">I&#8217;ve selected icons of 16px in size because it look better in content. If you need large icons, you will need to use the css display:block property. </p>
<p><b><br />
CSS styles<br />
</b><br />
Now we&#8217;ve have the right icons. Now we need to add classes with background image.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">a<span style="color: #6666ff;">.pdf</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/*The background image*/</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">images/pdf.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">center</span>;
	<span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span>;
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">16px</span>; <span style="color: #808080; font-style: italic;">/* To center the text vertically with the icon */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
a<span style="color: #6666ff;">.txt</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/*The background image*/</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">images/txt.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">center</span>;
	<span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span>;
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">16px</span>;
<span style="color: #00AA00;">&#125;</span>
&nbsp;
a<span style="color: #6666ff;">.zip</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/*The background image*/</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">images/zip.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">center</span>;
	<span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span>;
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">16px</span>;
<span style="color: #00AA00;">&#125;</span>
&nbsp;
a<span style="color: #6666ff;">.email</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">images/email.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">center</span>;
	<span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span>;
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">16px</span>;
<span style="color: #00AA00;">&#125;</span>
&nbsp;
a<span style="color: #6666ff;">.external</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">images/ext_link.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">center</span>;
	<span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span>;
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">16px</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p class="note" if you want to use large icons, you need to turn the anchor element display style to block. Otherwise, the images will be hidden at the edges.</p>
<p>Eg:</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">&nbsp;
<span style="color: #6666ff;">.download</span> a<span style="color: #6666ff;">.zip</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/*The background image*/</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">images/zip.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">center</span>;
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">48px</span>;
	<span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">55px</span>;
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">48px</span>; <span style="color: #808080; font-style: italic;">/* Center the text vertically with image */</span>
	<span style="color: #000000; font-weight: bold;">vertical-align</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">bottom</span>; <span style="color: #808080; font-style: italic;">/* to align the text with image bottom, line height property required */</span>
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span>; <span style="color: #808080; font-style: italic;">/* Need this to show the images fully */</span>
	<span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span>; <span style="color: #808080; font-style: italic;">/* You might need this as well for aligning it with the parent element */</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p><strong>JQuery</strong> </p>
<p>Now, we can write the jQuery statements.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</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>
&nbsp;
&nbsp;
	<span style="color: #006600; font-style: italic;">// Add pdf icons to pdf links</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a[href$='.pdf']&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;pdf&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #006600; font-style: italic;">// Add txt icons to document links (doc, rtf, txt)</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a[href$='.doc'], a[href$='.txt'], a[href$='.rft']&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;txt&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #006600; font-style: italic;">// Add zip icons to Zip file links (zip, rar)</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a[href$='.zip'], a[href$='.rar']&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;zip&quot;</span><span style="color: #009900;">&#41;</span>; 
&nbsp;
	<span style="color: #006600; font-style: italic;">// Add email icons to email links</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a[href^='mailto:']&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;email&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #006600; font-style: italic;">//Add external link icon to external links - </span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</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>
		<span style="color: #006600; font-style: italic;">//Compare the anchor tag's host name with location's host name</span>
	    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hostname</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hostname</span> <span style="color: #339933;">!==</span> location.<span style="color: #660066;">hostname</span>;
	  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;external&quot;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #006600; font-style: italic;">//You might also want to set the _target attribute to blank</span>
	<span style="color: #006600; font-style: italic;">/*
	$('a').filter(function() {
		//Compare the anchor tag's host name with location's host name
	    return this.hostname &amp;&amp; this.hostname !== location.hostname;
	  }).addClass(&quot;external&quot;).attr(&quot;target&quot;, &quot;_blank&quot;);
	*/</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p class="note">For determining the links, we&#8217;re using jQuery&#8217;s attribute syntax. For eg: To identify PDF links, we check whether the href attribute ends with a &#8216;.pdf&#8217; extension. </p>
<p>To find external links we&#8217;ll compare the anchor tag&#8217;s host name with the location host name </p>
<p class="wanrning">Do not copy paste from the above code. It is formatted for viewig. Please download the full source code at the end </p>
<p><strong>demo</strong><br />
It will look like the below image. For an actual demo check the below links.</p>
<p><img src="http://cool-javascripts.com/wp-content/uploads/2009/01/demo-links.jpg" /></p>
<p class="download"><strong>Demo &amp; Download</strong><br />
<a class="opensame" rel="shadowbox" href="http://www.demos.cool-javascripts.com/jquery-icons-to-links.html">Demo</a><br />
<a class="opennew" href="http://demos.cool-javascripts.com/jquery-icons-to-links.zip">Download full examples (24 KB) </a></p>


<p>Related posts:<ol><li><a href='http://cool-javascripts.com/jquery/vertical-alignment-of-contents-inside-an-element-using-jquery.html' rel='bookmark' title='Permanent Link: Vertical alignment of contents inside an element using jQuery'>Vertical alignment of contents inside an element using jQuery</a> <small>One of the old problems! Vertical alignment of contents inside...</small></li><li><a href='http://cool-javascripts.com/jquery/building-a-reusabe-font-size-controller-interface-using-jquery.html' rel='bookmark' title='Permanent Link: Building a reusabe font size controller interface using jQuery'>Building a reusabe font size controller interface using jQuery</a> <small>Most of the web2.0 design allows the user to change...</small></li><li><a href='http://cool-javascripts.com/effects/make-curved-corners-with-jquery-corner-plugin.html' rel='bookmark' title='Permanent Link: Make curved corners with jQuery Corner plugin'>Make curved corners with jQuery Corner plugin</a> <small>If you want a curved edge for your element, the...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://cool-javascripts.com/jquery/add-icons-to-your-links-automatically-using-jquery-css.html/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
