<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: A better data selector for jQuery</title>
	<atom:link href="http://james.padolsey.com/javascript/a-better-data-selector-for-jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://james.padolsey.com/javascript/a-better-data-selector-for-jquery/</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 02 Sep 2010 20:23:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Tobin</title>
		<link>http://james.padolsey.com/javascript/a-better-data-selector-for-jquery/comment-page-1/#comment-22349</link>
		<dc:creator>Tobin</dc:creator>
		<pubDate>Fri, 16 Oct 2009 03:35:15 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=775#comment-22349</guid>
		<description>Thanks James, I&#039;ll check it out.</description>
		<content:encoded><![CDATA[<p>Thanks James, I&#8217;ll check it out.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James</title>
		<link>http://james.padolsey.com/javascript/a-better-data-selector-for-jquery/comment-page-1/#comment-21773</link>
		<dc:creator>James</dc:creator>
		<pubDate>Wed, 07 Oct 2009 21:11:33 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=775#comment-21773</guid>
		<description>Just a note. I&#039;ve been testing this recently and it doesn&#039;t appear to work well with recent releases of jQuery. I&#039;m looking into it and will hopefully have an answer soon. For now, I suggest using the following &lt;code&gt;:data&lt;/code&gt; solution:

&lt;pre lang=&quot;javascript&quot;&gt;
jQuery.expr[&#039;:&#039;].data = function(elem, index, m) {

    m[0] = m[0].replace(/:data\(&#124;\)$/g, &#039;&#039;);

    var regex = new RegExp(&#039;([\&#039;&quot;]?)((?:\\\\\\1&#124;.)+?)\\1(,&#124;$)&#039;, &#039;g&#039;),
        key = regex.exec( m[0] )[2],
        val = regex.exec( m[0] )[2];
    
    return val ? jQuery(elem).data(key) == val : !!jQuery(elem).data(key);
    
};
&lt;/pre&gt;

Its usage is as follows:

&lt;pre lang=&quot;javascript&quot;&gt;
$(&#039;div&#039;).data(&#039;foo&#039;,&#039;bar&#039;);

// Select all elements with &quot;foo&quot; set to &quot;bar&quot;:
$(&#039;*:data(foo,bar)&#039;)

// With quotes (for complex strings):
$(&#039;div:data(&quot;foo&quot;,&quot;b a r&quot;)&#039;);
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Just a note. I&#8217;ve been testing this recently and it doesn&#8217;t appear to work well with recent releases of jQuery. I&#8217;m looking into it and will hopefully have an answer soon. For now, I suggest using the following <code>:data</code> solution:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span class="me1">expr</span><span class="br0">&#91;</span><span class="st0">':'</span><span class="br0">&#93;</span>.<span class="me1">data</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span>elem<span class="sy0">,</span> index<span class="sy0">,</span> m<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp;
    m<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="sy0">=</span> m<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>.<span class="me1">replace</span><span class="br0">&#40;</span><span class="co2">/:data\(|\)$/g</span><span class="sy0">,</span> <span class="st0">''</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
    <span class="kw2">var</span> regex <span class="sy0">=</span> <span class="kw2">new</span> RegExp<span class="br0">&#40;</span><span class="st0">'([<span class="es0">\'</span>&quot;]?)((?:<span class="es0">\\</span><span class="es0">\\</span><span class="es0">\\</span>1|.)+?)<span class="es0">\\</span>1(,|$)'</span><span class="sy0">,</span> <span class="st0">'g'</span><span class="br0">&#41;</span><span class="sy0">,</span>
        key <span class="sy0">=</span> regex.<span class="me1">exec</span><span class="br0">&#40;</span> m<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span><span class="sy0">,</span>
        val <span class="sy0">=</span> regex.<span class="me1">exec</span><span class="br0">&#40;</span> m<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
    <span class="kw1">return</span> val <span class="sy0">?</span> jQuery<span class="br0">&#40;</span>elem<span class="br0">&#41;</span>.<span class="me1">data</span><span class="br0">&#40;</span>key<span class="br0">&#41;</span> <span class="sy0">==</span> val <span class="sy0">:</span> <span class="sy0">!!</span>jQuery<span class="br0">&#40;</span>elem<span class="br0">&#41;</span>.<span class="me1">data</span><span class="br0">&#40;</span>key<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="br0">&#125;</span><span class="sy0">;</span></pre></div></div>

<p>Its usage is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">'div'</span><span class="br0">&#41;</span>.<span class="me1">data</span><span class="br0">&#40;</span><span class="st0">'foo'</span><span class="sy0">,</span><span class="st0">'bar'</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Select all elements with &quot;foo&quot; set to &quot;bar&quot;:</span>
$<span class="br0">&#40;</span><span class="st0">'*:data(foo,bar)'</span><span class="br0">&#41;</span>
&nbsp;
<span class="co1">// With quotes (for complex strings):</span>
$<span class="br0">&#40;</span><span class="st0">'div:data(&quot;foo&quot;,&quot;b a r&quot;)'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Tobin</title>
		<link>http://james.padolsey.com/javascript/a-better-data-selector-for-jquery/comment-page-1/#comment-21769</link>
		<dc:creator>Tobin</dc:creator>
		<pubDate>Wed, 07 Oct 2009 19:43:53 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=775#comment-21769</guid>
		<description>Has there been any more talk of this feature being added?  I just found myself needing it today, and I&#039;m having trouble adapting the initial code to work with the data: approach found in the comments.</description>
		<content:encoded><![CDATA[<p>Has there been any more talk of this feature being added?  I just found myself needing it today, and I&#8217;m having trouble adapting the initial code to work with the data: approach found in the comments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Naro-1</title>
		<link>http://james.padolsey.com/javascript/a-better-data-selector-for-jquery/comment-page-1/#comment-18644</link>
		<dc:creator>Naro-1</dc:creator>
		<pubDate>Wed, 19 Aug 2009 21:16:26 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=775#comment-18644</guid>
		<description>this is just awesome. exactly what i&#039;ve been looking for ! Many thanks for this quick hack.</description>
		<content:encoded><![CDATA[<p>this is just awesome. exactly what i&#8217;ve been looking for ! Many thanks for this quick hack.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James</title>
		<link>http://james.padolsey.com/javascript/a-better-data-selector-for-jquery/comment-page-1/#comment-10580</link>
		<dc:creator>James</dc:creator>
		<pubDate>Sun, 26 Apr 2009 15:33:44 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=775#comment-10580</guid>
		<description>@Balazs, thanks for the links. I tried searching on Google for something similar before publishing this post but the search was fruitless. Nice plugin you&#039;ve got there! I definitely like the idea of a prefix on the data selector, it would certainly eliminate the attribute problem. Thanks :)


@Kyle, I agree; from a code maintainability point of view, the previous data selector is definitely superior. Maybe we could meet halfway with a &#039;:&#039; or even a &#039;data:&#039; prefix like Balazs suggested? That would definitely make it more explicit:

&lt;pre lang=&quot;javascript&quot;&gt;
jQuery(&#039;a[data:ABC=123]&#039;);
&lt;/pre&gt;

Plus, the new implementation ties in very nicely with Sizzle&#039;s core and so is slightly faster than the older &#039;:data()&#039; one. It&#039;s really a matter of taste in the end... go with whatever seems more intuitive.</description>
		<content:encoded><![CDATA[<p>@Balazs, thanks for the links. I tried searching on Google for something similar before publishing this post but the search was fruitless. Nice plugin you&#8217;ve got there! I definitely like the idea of a prefix on the data selector, it would certainly eliminate the attribute problem. Thanks <img src='http://james.padolsey.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>@Kyle, I agree; from a code maintainability point of view, the previous data selector is definitely superior. Maybe we could meet halfway with a &#8216;:&#8217; or even a &#8216;data:&#8217; prefix like Balazs suggested? That would definitely make it more explicit:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery<span class="br0">&#40;</span><span class="st0">'a[data:ABC=123]'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>Plus, the new implementation ties in very nicely with Sizzle&#8217;s core and so is slightly faster than the older &#8216;:data()&#8217; one. It&#8217;s really a matter of taste in the end&#8230; go with whatever seems more intuitive.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Balazs Endresz</title>
		<link>http://james.padolsey.com/javascript/a-better-data-selector-for-jquery/comment-page-1/#comment-10499</link>
		<dc:creator>Balazs Endresz</dc:creator>
		<pubDate>Sat, 25 Apr 2009 14:58:48 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=775#comment-10499</guid>
		<description>Oops, that doesn&#039;t work unless you modify some regex too:
&lt;pre lang=&quot;javascript&quot;&gt;
var r = jQuery.expr.match.ATTR.source;
jQuery.expr.match.ATTR = new RegExp( r.substr(0,6) + &quot;\\:?&quot; + r.substr(6) );
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Oops, that doesn&#8217;t work unless you modify some regex too:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> r <span class="sy0">=</span> jQuery.<span class="me1">expr</span>.<span class="me1">match</span>.<span class="me1">ATTR</span>.<span class="me1">source</span><span class="sy0">;</span>
jQuery.<span class="me1">expr</span>.<span class="me1">match</span>.<span class="me1">ATTR</span> <span class="sy0">=</span> <span class="kw2">new</span> RegExp<span class="br0">&#40;</span> r.<span class="me1">substr</span><span class="br0">&#40;</span><span class="nu0">0</span><span class="sy0">,</span><span class="nu0">6</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">&quot;<span class="es0">\\</span>:?&quot;</span> <span class="sy0">+</span> r.<span class="me1">substr</span><span class="br0">&#40;</span><span class="nu0">6</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Balazs Endresz</title>
		<link>http://james.padolsey.com/javascript/a-better-data-selector-for-jquery/comment-page-1/#comment-10498</link>
		<dc:creator>Balazs Endresz</dc:creator>
		<pubDate>Sat, 25 Apr 2009 14:50:39 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=775#comment-10498</guid>
		<description>Just for the record, this has been already come up on the jquery-dev list a while ago:
http://groups.google.com/group/jquery-dev/browse_thread/thread/d74e5a88b9649d24

there&#039;s already a ticket for it as well:
http://dev.jquery.com/ticket/3586

and I also made a plugin a few months ago that does this a bit differently:
http://plugins.jquery.com/project/EnhancedAttributeSelectors


Anyway, this implementation is &lt;b&gt;very&lt;/b&gt; elegant, and doesn&#039;t add as much overhead as mine at all. What I would only suggest is using a prefix so there&#039;s no ambiguity if you&#039;re dealing with an attribute or data:

&lt;pre lang=&quot;javascript&quot;&gt; 
            $.expr.attrHandle[&#039;:&#039; + key] = function(elem){
                return $(elem).attr(key) &#124;&#124; $(elem).data(key);
            };
&lt;/pre&gt;

then you can select by data like this: &lt;code&gt; $(&#039;a[:ABC=123]&#039;); &lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Just for the record, this has been already come up on the jquery-dev list a while ago:<br />
<a href="http://groups.google.com/group/jquery-dev/browse_thread/thread/d74e5a88b9649d24">http://groups.google.com/group/jquery-dev/browse_thread/thread/d74e5a88b9649d24</a></p>
<p>there&#8217;s already a ticket for it as well:<br />
<a href="http://dev.jquery.com/ticket/3586">http://dev.jquery.com/ticket/3586</a></p>
<p>and I also made a plugin a few months ago that does this a bit differently:<br />
<a href="http://plugins.jquery.com/project/EnhancedAttributeSelectors">http://plugins.jquery.com/project/EnhancedAttributeSelectors</a></p>
<p>Anyway, this implementation is <b>very</b> elegant, and doesn&#8217;t add as much overhead as mine at all. What I would only suggest is using a prefix so there&#8217;s no ambiguity if you&#8217;re dealing with an attribute or data:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">            $.<span class="me1">expr</span>.<span class="me1">attrHandle</span><span class="br0">&#91;</span><span class="st0">':'</span> <span class="sy0">+</span> key<span class="br0">&#93;</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span>elem<span class="br0">&#41;</span><span class="br0">&#123;</span>
                <span class="kw1">return</span> $<span class="br0">&#40;</span>elem<span class="br0">&#41;</span>.<span class="me1">attr</span><span class="br0">&#40;</span>key<span class="br0">&#41;</span> <span class="sy0">||</span> $<span class="br0">&#40;</span>elem<span class="br0">&#41;</span>.<span class="me1">data</span><span class="br0">&#40;</span>key<span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span><span class="sy0">;</span></pre></div></div>

<p>then you can select by data like this: <code> $('a[:ABC=123]'); </code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jack F</title>
		<link>http://james.padolsey.com/javascript/a-better-data-selector-for-jquery/comment-page-1/#comment-10496</link>
		<dc:creator>Jack F</dc:creator>
		<pubDate>Sat, 25 Apr 2009 14:35:28 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=775#comment-10496</guid>
		<description>Thanks for answering my Question James. With some thought I think I agree with Kyle, when adding the &quot;:data&quot; bit it is much more clear what is going on. If this feature is to be included in jQuery (I&#039;ll vote for it!) then I think that the &quot;:data&quot; version should be added.

Jack.</description>
		<content:encoded><![CDATA[<p>Thanks for answering my Question James. With some thought I think I agree with Kyle, when adding the &#8220;:data&#8221; bit it is much more clear what is going on. If this feature is to be included in jQuery (I&#8217;ll vote for it!) then I think that the &#8220;:data&#8221; version should be added.</p>
<p>Jack.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rey Bango</title>
		<link>http://james.padolsey.com/javascript/a-better-data-selector-for-jquery/comment-page-1/#comment-10427</link>
		<dc:creator>Rey Bango</dc:creator>
		<pubDate>Fri, 24 Apr 2009 19:10:46 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=775#comment-10427</guid>
		<description>@James: Well just so you know, John Resig thought it was pretty neat when I showed it to him :)</description>
		<content:encoded><![CDATA[<p>@James: Well just so you know, John Resig thought it was pretty neat when I showed it to him <img src='http://james.padolsey.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kyle</title>
		<link>http://james.padolsey.com/javascript/a-better-data-selector-for-jquery/comment-page-1/#comment-10417</link>
		<dc:creator>Kyle</dc:creator>
		<pubDate>Fri, 24 Apr 2009 17:25:04 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=775#comment-10417</guid>
		<description>I personally think that the &quot;:data&quot; selector is a nicer implementation.  It makes it more self evident where the variable and value are coming from.  The bracket selector &quot;[]&quot; is a standard for specifically selecting attributes, I don&#039;t know if it&#039;s good to be mixing something else with that.

If I had to jump into someone elses code and they were using this, I probably would go crazy trying to figure out how they are getting values from attributes that aren&#039;t there.  I&#039;d probably have no clue that they have this extra little snippet at the bottom of the js file somewhere.</description>
		<content:encoded><![CDATA[<p>I personally think that the &#8220;:data&#8221; selector is a nicer implementation.  It makes it more self evident where the variable and value are coming from.  The bracket selector &#8220;[]&#8221; is a standard for specifically selecting attributes, I don&#8217;t know if it&#8217;s good to be mixing something else with that.</p>
<p>If I had to jump into someone elses code and they were using this, I probably would go crazy trying to figure out how they are getting values from attributes that aren&#8217;t there.  I&#8217;d probably have no clue that they have this extra little snippet at the bottom of the js file somewhere.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
