Within a selector, how do I check if a value of an attribute contains 'a', 'b', or 'c' in a single line? Can you do for example: $('input[name]=a|b|c')
? Well, I tested that and it didn't work.
views:
56answers:
2
+1
A:
$("input[name='a'],input[name='b'],input[name='c']")
If this string seems too long or redundant, you could build up the selector like :
var names = ['a','b','c'];
var selector = [];
for (var i=0; i<names.length; i++) {
selector.push("input[name='" + names[i] + "']");
}
$(selector.join(','))
Yanick Rochon
2010-09-06 02:35:33
For reference, that's an example of the multiple selector. See the jQuery selector API, http://api.jquery.com/category/selectors/
michaeltwofish
2010-09-06 03:27:42
A:
If nothing but a regex will do, you can try using .filter()
:
$('#somecontainer input').filter( function(){
return this.name.match(/a|b|c/);
});
Another of the many ways to do this is to assemble a jQuery object with .add()
. And if you really mean contains rather than equals, don't forget to use name*=a
.
$('input[name*=a]')
.add('input[name*=b]')
.add('input[name*=c]')
.add('<p>something</p>') // you can .add() almost anything. useful.
Ken Redler
2010-09-06 04:13:07
The regex part is very interesting. Given the clues, I found this plugin: http://james.padolsey.com/javascript/regex-selector-for-jquery/
Rainmasterrr
2010-09-06 04:56:10