<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Magp.ie &#187; jQuery</title>
	<atom:link href="http://magp.ie/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://magp.ie</link>
	<description>A nest for the random, shiny, online tidbits I stumble across...</description>
	<lastBuildDate>Wed, 16 May 2012 09:40:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='magp.ie' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/061e340c5da13b5a41ae8016bee03aa8?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Magp.ie &#187; jQuery</title>
		<link>http://magp.ie</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://magp.ie/osd.xml" title="Magp.ie" />
	<atom:link rel='hub' href='http://magp.ie/?pushpress=hub'/>
		<item>
		<title>HTML5 Data attributes in HTML and jQuery</title>
		<link>http://magp.ie/2011/11/29/html5-data-attributes-in-html-and-jquery/</link>
		<comments>http://magp.ie/2011/11/29/html5-data-attributes-in-html-and-jquery/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 18:18:41 +0000</pubDate>
		<dc:creator>Eoin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Guides]]></category>
		<category><![CDATA[data attribute]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[json_encode]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://magp.ie/?p=667</guid>
		<description><![CDATA[When writing javascript, it is often necessary to include metadata in the HTML markup, to help define some element or behaviour. There are common options available. You can use hidden inputs and/or standard attributes like class or title to store &#8230; <a href="http://magp.ie/2011/11/29/html5-data-attributes-in-html-and-jquery/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&#038;blog=11708208&#038;post=667&#038;subd=blogalhost&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When writing javascript, it is often necessary to include metadata in the HTML markup, to help define some element or behaviour. There are common options available. You can use hidden inputs and/or standard attributes like <code>class</code> or <code>title</code> to store this metadata. However with HTML5&#8242;s data attribute, storing and parsing this data has become a whole lot easier and cleaner.</p>
<p>The syntax is straightforward. Any attribute prefixed with <code>data-</code> will be treated as data storage.</p>
<p><pre class="brush: xml;">&lt;div class=&quot;test&quot; data-foo=&quot;bar&quot;&gt;&lt;/div&gt;</pre></p>
<p>jQuery accesses this data like so&#8230;<br />
<pre class="brush: jscript;">var data = $( 'div.test' ).data( 'foo' ); // returns bar</pre></p>
<p>Support for the data attribute has been added since jQuery <a href="http://blog.jquery.com/2010/10/16/jquery-143-released/" title="version 1.4.3" target="_blank">version 1.4.3</a>. jQuery&#8217;s implementation is smart enough that it can parse the attribute easily and even determine the correct data type used.</p>
<p>What I have found really useful is the fact that the jQuery can parse JSON syntax and return a JSON object. This makes passing data in PHP trivial, using the <a href="http://php.net/manual/en/function.json-encode.php" title="json_encode" target="_blank">json_encode</a> method. We also need to use <a href="http://php.net/manual/en/function.htmlspecialchars.php" title="Escape quotes" target="_blank">htmlspecialchars</a> method to escape or convert any quotes in the JSON string.<br />
<pre class="brush: php;">
&lt;?php  
$test = array( 'row' =&gt; 1, 'col' =&gt; 6, 'color' =&gt; 'pink' ); //create array of data you want to pass to jquery
$test = json_encode( $test ); //convert array to a JSON string
$test = htmlspecialchars( $test, ENT_QUOTES ); //convert any quotes into HTML entities so JSON string behaves as a proper HTML attribute.
?&gt;
&lt;div class=&quot;test&quot; data-complex=&quot;&lt;?php echo $test ; ?&gt;&quot;&gt;&lt;/div&gt;</pre></p>
<p>The jQuery parses the JSON string like so&#8230;<br />
<pre class="brush: jscript;">var test = $( 'div.test' ).data( 'complex' ); // returns JSON Object

console.log( test.color ); // outputs pink!</pre></p>
<p>Important to note that this method is also backward compatible with older browsers, so there is no excuse not to give it a go!</p>
<br />Filed under: <a href='http://magp.ie/category/code/'>Code</a>, <a href='http://magp.ie/category/guides/'>Guides</a> Tagged: <a href='http://magp.ie/tag/data-attribute/'>data attribute</a>, <a href='http://magp.ie/tag/html5/'>html5</a>, <a href='http://magp.ie/tag/jquery/'>jQuery</a>, <a href='http://magp.ie/tag/json/'>json</a>, <a href='http://magp.ie/tag/json_encode/'>json_encode</a>, <a href='http://magp.ie/tag/php/'>php</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/blogalhost.wordpress.com/667/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blogalhost.wordpress.com/667/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/blogalhost.wordpress.com/667/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/blogalhost.wordpress.com/667/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/blogalhost.wordpress.com/667/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/blogalhost.wordpress.com/667/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/blogalhost.wordpress.com/667/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/blogalhost.wordpress.com/667/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/blogalhost.wordpress.com/667/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/blogalhost.wordpress.com/667/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/blogalhost.wordpress.com/667/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/blogalhost.wordpress.com/667/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/blogalhost.wordpress.com/667/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/blogalhost.wordpress.com/667/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&#038;blog=11708208&#038;post=667&#038;subd=blogalhost&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://magp.ie/2011/11/29/html5-data-attributes-in-html-and-jquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<georss:point>53.734750 -8.989992</georss:point>
		<geo:lat>53.734750</geo:lat>
		<geo:long>-8.989992</geo:long>
		<media:content url="http://1.gravatar.com/avatar/72dd449e5e79e046c1c09ed8712b525a?s=96&#38;d=monsterid&#38;r=PG" medium="image">
			<media:title type="html">eoigal</media:title>
		</media:content>
	</item>
		<item>
		<title>How to check if an element exists in jQuery</title>
		<link>http://magp.ie/2011/08/03/how-to-check-if-an-element-exists-in-jquery/</link>
		<comments>http://magp.ie/2011/08/03/how-to-check-if-an-element-exists-in-jquery/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 20:02:56 +0000</pubDate>
		<dc:creator>Eoin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[exists]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[selector]]></category>

		<guid isPermaLink="false">http://magp.ie/?p=606</guid>
		<description><![CDATA[Briefer again&#8230; via http://aaronrussell.co.uk/legacy/check-if-an-element-exists-using-jquery/ Filed under: Code Tagged: exists, jQuery, selector<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&#038;blog=11708208&#038;post=606&#038;subd=blogalhost&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><pre class="brush: jscript;">
if ( $(&quot;#mydiv&quot;).length ){
  // do something here
}
</pre></p>
<p>Briefer again&#8230;</p>
<p><pre class="brush: jscript;">
if ( $(&quot;#mydiv&quot;)[0] ){
  // do something here
}
</pre></p>
<p>via <a title="check-if-an-element-exists-using-jquery" href="http://aaronrussell.co.uk/legacy/check-if-an-element-exists-using-jquery/" target="_blank">http://aaronrussell.co.uk/legacy/check-if-an-element-exists-using-jquery/</a></p>
<br />Filed under: <a href='http://magp.ie/category/code/'>Code</a> Tagged: <a href='http://magp.ie/tag/exists/'>exists</a>, <a href='http://magp.ie/tag/jquery/'>jQuery</a>, <a href='http://magp.ie/tag/selector/'>selector</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/blogalhost.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blogalhost.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/blogalhost.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/blogalhost.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/blogalhost.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/blogalhost.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/blogalhost.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/blogalhost.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/blogalhost.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/blogalhost.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/blogalhost.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/blogalhost.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/blogalhost.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/blogalhost.wordpress.com/606/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&#038;blog=11708208&#038;post=606&#038;subd=blogalhost&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://magp.ie/2011/08/03/how-to-check-if-an-element-exists-in-jquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<georss:point>53.734750 -8.989992</georss:point>
		<geo:lat>53.734750</geo:lat>
		<geo:long>-8.989992</geo:long>
		<media:content url="http://1.gravatar.com/avatar/72dd449e5e79e046c1c09ed8712b525a?s=96&#38;d=monsterid&#38;r=PG" medium="image">
			<media:title type="html">eoigal</media:title>
		</media:content>
	</item>
		<item>
		<title>Auto highlight text inside pre tags using jQuery</title>
		<link>http://magp.ie/2010/04/07/auto-highlight-text-inside-pre-tags-using-jquery/</link>
		<comments>http://magp.ie/2010/04/07/auto-highlight-text-inside-pre-tags-using-jquery/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 19:31:45 +0000</pubDate>
		<dc:creator>Eoin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[auto-select]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[pre tags]]></category>

		<guid isPermaLink="false">http://magp.ie/?p=168</guid>
		<description><![CDATA[We use pre tags in the Polldaddy plugin to display the embed codes and short URLs. We use pre tags because it formats the text inside the tags as we insert it, which is ideal for displaying code in particular. &#8230; <a href="http://magp.ie/2010/04/07/auto-highlight-text-inside-pre-tags-using-jquery/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&#038;blog=11708208&#038;post=168&#038;subd=blogalhost&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We use pre tags in the <a href="http://wordpress.org/extend/plugins/polldaddy/" target="_blank">Polldaddy plugin</a> to display the embed codes and short URLs. We use <a href="http://www.w3schools.com/TAGS/tag_pre.asp" target="_blank">pre</a> tags because it formats the text inside the tags as we insert it, which is ideal for displaying code in particular. The thing is, people like to copy this code and they have to go to the <i>awful</i> bother of selecting all the text manually if they wish to copy the code or URL.</p>
<p>The alternative is to use an text input or text area which will allow you to use a select method that you can call on a click event, but then you lose the neat formatting that pre tags offer.</p>
<p>So I had a look for a jQuery hack to auto select all the text inside the pre tags. The only complication with this appears to be a cross platform solution as each browser appears to have their own way of selecting text.<br />
<span id="more-168"></span><br />
<strong>Internet Explorer</strong> uses <a href="http://msdn.microsoft.com/en-us/library/ms536401%28VS.85%29.aspx" target="_blank">createTextRange</a>.<br />
<strong>Opera</strong> and <strong>Firefox</strong> use <a href="http://www.wrox.com/WileyCDA/Section/JavaScript-DOM-Ranges.id-292301.html" target="_blank">createRange</a>.<br />
<strong>Safari</strong> uses <a href="http://developer.apple.com/safari/library/documentation/AppleApplications/Reference/WebKitDOMRef/DOMSelection_idl/Classes/DOMSelection/index.html" target="_blank">DOMSelection</a>.</p>
<p>So with a pre tag like &#8230;<br />
<pre class="brush: plain;">
&lt;pre class='code'&gt;http://poll.fm/11w3q&lt;/pre&gt;
</pre></p>
<p>&#8230; the following code will auto-select the all the text between the tags &#8230;<br />
<pre class="brush: jscript;">
	jQuery( document ).ready(function() {	
		jQuery( 'pre.code' ).click( function() {
			var refNode = $( this )[0];
			if ( $.browser.msie ) {
				var range = document.body.createTextRange();
				range.moveToElementText( refNode );
				range.select();
			} else if ( $.browser.mozilla || $.browser.opera ) {
				var selection = window.getSelection();
				var range = document.createRange();
				range.selectNodeContents( refNode );
				selection.removeAllRanges();
				selection.addRange( range );
			} else if ( $.browser.safari ) {
				var selection = window.getSelection();
				selection.setBaseAndExtent( refNode, 0, refNode, 1 );
			}
		} );
  	} );
</pre></p>
<br />Filed under: <a href='http://magp.ie/category/code/'>Code</a> Tagged: <a href='http://magp.ie/tag/auto-select/'>auto-select</a>, <a href='http://magp.ie/tag/jquery/'>jQuery</a>, <a href='http://magp.ie/tag/pre-tags/'>pre tags</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/blogalhost.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blogalhost.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/blogalhost.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/blogalhost.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/blogalhost.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/blogalhost.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/blogalhost.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/blogalhost.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/blogalhost.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/blogalhost.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/blogalhost.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/blogalhost.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/blogalhost.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/blogalhost.wordpress.com/168/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&#038;blog=11708208&#038;post=168&#038;subd=blogalhost&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://magp.ie/2010/04/07/auto-highlight-text-inside-pre-tags-using-jquery/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<georss:point>53.734750 -8.989992</georss:point>
		<geo:lat>53.734750</geo:lat>
		<geo:long>-8.989992</geo:long>
		<media:content url="http://1.gravatar.com/avatar/72dd449e5e79e046c1c09ed8712b525a?s=96&#38;d=monsterid&#38;r=PG" medium="image">
			<media:title type="html">eoigal</media:title>
		</media:content>
	</item>
	</channel>
</rss>
