tags:

views:

27

answers:

3

I have the following query:

$("input:text").somefunction();

How can I also include a button and Textarea in the above code without using classes?

+1  A: 

You can include extra elements by adding commas.

$('#myForm input:text:eq(9), #myForm input:button, #myForm input:textarea').someFunction();
g.d.d.c
+1  A: 

:text filters the inputs for text-boxes, so simply selecting :input will get what you're after:

Selects all input, textarea, select and button elements.

note that this is a bit confusing: $('input') selects all <input> elements (buttons too, but not textareas), but $(':input') gets what you want.

Kobi
Can you suggest how would I do it using class?
ScG
@ScG - if you can change the source, you just add a class the the elements you need, eg `<input class="Button SomeFunctionable">`, and select `$('.SomeFunctionable')`. Remember that an element can have multiple classes.
Kobi
+1  A: 

Besides the usual approach of expanding your selector with commas, you can use add:

$('#form :input:text:eq(9)')
    .add('#form textarea')
    .add('#form :input:button')
    .add('anything_else')
    .somefunction();

Note however that :input alone will select all your form elements.

Reference: :input selector

Ken Redler
This is a bit backwards, if I may, you select all `:input` elements, break them by type, and join them back together. Also, there's no `:textarea` selector. Good tip about `add` though.
Kobi
@Kobi, yup, thanks, you're right about textarea. Overzealous pasting. Fixed. Also I agree it's an odd way to do it, but I plopped in that last "anything else" line just to illustrate that it could be a nice way to accumulate a bunch of disparate pieces, syntactically.
Ken Redler