Created
January 2, 2011 22:08
-
-
Save bibby/762866 to your computer and use it in GitHub Desktop.
jquery input "array key" selector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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(''))); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i want to see a demo. Thanks!