Skip to content

Instantly share code, notes, and snippets.

@bibby
Created January 2, 2011 22:08
Show Gist options
  • Save bibby/762866 to your computer and use it in GitHub Desktop.
Save bibby/762866 to your computer and use it in GitHub Desktop.
jquery input "array key" selector
/*
The past few days, I've seen people in #jquery asking about using selectors
to match inputs with names that would produce arrays on the backend, such as
<input name="foo[]" />
<input name="foo[foo]" />
<input name="foo[bar][baz]" />
So I wrote a selector that works with these.
Feel free to use/improve
A demo is up at:
http://bbby.org/share/jquery_key_selector.php
*/
jQuery.extend(jQuery.expr[':'],{
key:function(elm,i,filter)
{
if(typeof elm.name == undefined)
return false;
var qual = filter[3],
name = elm.name;
if(!name.match(/\[.*\]/))
return false;
if(qual == undefined)
return true;
var keys=qual.split(/,/g),
l=keys.length;
while(l--)
keys[l] = "\\["+(keys[l].replace(/^\s+|\s+$/g,''))+"\\]";
return name.match(new RegExp(keys.join('')));
}
});
@olaferlandsen
Copy link

i want to see a demo. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment