views:

56

answers:

2

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.

+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
For reference, that's an example of the multiple selector. See the jQuery selector API, http://api.jquery.com/category/selectors/
michaeltwofish
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
The regex part is very interesting. Given the clues, I found this plugin: http://james.padolsey.com/javascript/regex-selector-for-jquery/
Rainmasterrr