<?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>camwebdesign.com &#187; jQuery</title>
	<atom:link href="http://camwebdesign.com/category/techniques/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://camwebdesign.com</link>
	<description>High quality, cost effective wed design and hosting solutions</description>
	<lastBuildDate>Mon, 12 Jul 2010 10:54:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Select code in snippets with jQuery</title>
		<link>http://camwebdesign.com/techniques/select-code-in-snippets-with-jquery/</link>
		<comments>http://camwebdesign.com/techniques/select-code-in-snippets-with-jquery/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 02:55:13 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[Techniques]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://camwebdesign.com/?p=445</guid>
		<description><![CDATA[This tutorial will show we use jQuery to create &#8220;Select Code&#8221; links in our Code Sippets. Before jumping into this jQuery technique, you may want to see our tutorial on how we construct our Consistent Cross-Browser Code Snippets. Building on our Code Snippet technique we add some HTML, namely a wrapping &#60;div&#62; and a &#60;span&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will show we use jQuery to create &#8220;Select Code&#8221; links in our Code Sippets.</p>
<p>Before jumping into this <a href="http://jquery.com/">jQuery</a> technique, you may want to see our tutorial on how we construct our <a href="/techniques/consistent-cross-browser-code-snippets/">Consistent Cross-Browser Code Snippets</a>.</p>
<p>Building on our Code Snippet technique we add <span id="more-445"></span>some HTML, namely a wrapping &lt;div&gt; and a &lt;span&gt; that holds our Select Code link.</p>
<div><span class="CODEselect">[<a href="#">Select Code</a>]</span></p>
<pre><code class="html" id="code1">&lt;div&gt;&lt;span class="CODEselect"&gt;[&lt;a href="#"&gt;Select Code&lt;/a&gt;]&lt;/span&gt;
&lt;pre&gt;&lt;code&gt;
&lt;!-- Place Code Here --&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;</code></pre>
</div>
<p>We style and hide our &lt;span&gt; with some CSS.</p>
<div><span class="CODEselect">[<a href="#">Select Code</a>]</span></p>
<pre><code class="css" id="code2">.CODEselect {
  display: none;
}

.CODEselect a:hover{
  color: #F66;
  text-decoration:underline;
}</code></pre>
</div>
<p>We add a layer of javascript and jQuery to the mix.  This javascript is attributed to david_kw from <a href="http://www.codingforums.com/archive/index.php/t-105808.html">this post</a> on codingforums.com.  The Select Code links won&#8217;t work without javascript and jQuery, so we let jQuery make the links visible with the .show() call, for those users with javascript enabled.  Then we set the .click() event of the links to fire the javascript function through a little <a href="http://docs.jquery.com/Traversing">jQuery Traversing</a> to find the correct Code Snippet area to select.</p>
<div><span class="CODEselect">[<a href="#">Select Code</a>]</span></p>
<pre><code class="html" id="code3">function CODEselect(thisID){
  var thisCODE = document.getElementById(thisID);
  if (window.getSelection) {
    var selection = window.getSelection();
    if (selection.setBaseAndExtent) { /* for Safari */
      selection.setBaseAndExtent(thisCODE, 0, thisCODE, 1);
    } else { /* for FF, Opera */
      var range = document.createRange();
      range.selectNodeContents(thisCODE);
      selection.removeAllRanges();
      selection.addRange(range);
    }
  } else { /* for IE */
    var range = document.body.createTextRange();
    range.moveToElementText(thisCODE);
    range.select();
  }
}
$(document).ready(function(){
  $('.CODEselect').show();
  $('.CODEselect a').click(function () {
      var ParentTagName = $(this).parents().get(1).tagName;
      var CodeBoxID = $(this).closest(ParentTagName).find('code').attr('id');
      CODEselect(CodeBoxID);
      return false;
    });
});</code></pre>
</div>
<p>Post your thoughts below&#8230;</p>
<hr /><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://camwebdesign.com/techniques/php-image-manipulation-functions/" rel="bookmark" title="January 9, 2010">PHP Image Manipulation Functions</a></li>
<li><a href="http://camwebdesign.com/techniques/consistent-cross-browser-code-snippets/" rel="bookmark" title="January 6, 2010">Consistent Cross-Browser Code Snippets</a></li>
<li><a href="http://camwebdesign.com/techniques/css-reset-anyone/" rel="bookmark" title="January 5, 2010">CSS Reset anyone?</a></li>
</ul>
<p><!-- Similar Posts took 14.744 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/select-code-in-snippets-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
