Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've created a new <code>data</code> selector that should enable you to do nested querying and AND conditions. Usage:</p> <pre><code>$('a:data(category==music,artist.name==Madonna)'); </code></pre> <p>The pattern is:</p> <pre><code>:data( {namespace} [{operator} {check}] ) </code></pre> <p>"operator" and "check" are optional. So, if you only have <code>:data(a.b.c)</code> it will simply check for the <em>truthiness</em> of <code>a.b.c</code>.</p> <p>You can see the available operators in the code below. Amongst them is <code>~=</code> which allows regex testing:</p> <pre><code>$('a:data(category~=^mus..$,artist.name~=^M.+a$)'); </code></pre> <p>I've tested it with a few variations and it seems to work quite well. I'll probably add this as a Github repo soon (with a full test suite), so keep a look out!</p> <p>The code:</p> <pre><code>(function(){ var matcher = /\s*(?:((?:(?:\\\.|[^.,])+\.?)+)\s*([!~&gt;&lt;=]=|[&gt;&lt;])\s*("|')?((?:\\\3|.)*?)\3|(.+?))\s*(?:,|$)/g; function resolve(element, data) { data = data.match(/(?:\\\.|[^.])+(?=\.|$)/g); var cur = jQuery.data(element)[data.shift()]; while (cur &amp;&amp; data[0]) { cur = cur[data.shift()]; } return cur || undefined; } jQuery.expr[':'].data = function(el, i, match) { matcher.lastIndex = 0; var expr = match[3], m, check, val, allMatch = null, foundMatch = false; while (m = matcher.exec(expr)) { check = m[4]; val = resolve(el, m[1] || m[5]); switch (m[2]) { case '==': foundMatch = val == check; break; case '!=': foundMatch = val != check; break; case '&lt;=': foundMatch = val &lt;= check; break; case '&gt;=': foundMatch = val &gt;= check; break; case '~=': foundMatch = RegExp(check).test(val); break; case '&gt;': foundMatch = val &gt; check; break; case '&lt;': foundMatch = val &lt; check; break; default: if (m[5]) foundMatch = !!val; } allMatch = allMatch === null ? foundMatch : allMatch &amp;&amp; foundMatch; } return allMatch; }; }()); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload