George Irimiciuc
George Irimiciuc

Reputation: 4633

Multiple selector

I know I can have multiple selections like this:

 $("input,submit,textarea")

Is there a way to bind these 3 together

$('input[name="input"]')
$('input[name="submit"]')
$('input[name="textarea"]')

or do I have to write it like

 $('input[name="input"],input[name="submit"],input[name="textarea"]')

?

I'm looking for something like $('input[name="input"&"submit"&"textarea"](this is not valid, of course), to not write 'input' 3 times.

Upvotes: 1

Views: 58

Answers (1)

Karl-André Gagnon
Karl-André Gagnon

Reputation: 33880

There is no "simple way" to achieve what you want. Writing them one by one is probably the best solution if you don't want to alter the DOM and save performance, but if you have a lot of element, it can be a real pain to write them all.

The best solution really is to group them with a common class and you it to select all related element.

In case you don't want that, you could use the filter method, but then, it will be slower cause it loop on all inputs.

$('input[name]').filter(function(){
    return $.inArray(this.name, ['test1','test2', '...']) > -1;
});

All you have to do is to write the different input name in the array.

Upvotes: 1

Related Questions