Reputation: 13
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.
Upvotes: 1
Views: 1663
Reputation: 23943
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.
Upvotes: 0
Reputation: 53616
$("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(','))
Upvotes: 1