views:

160

answers:

3

Which is the easiest way to get all form elements which are contained by a wrapper element.

<form name="myForm">
  <input name="elementA" />
  <div id="wrapper">
    <input name="elementB" />
    <textarea name="elementC" />
  </div>
</form>

In the above HTML I would elementB and elementC but not elementA. I do not want to list all form element types (select,textarea,input,option...). I would prefer to use myForm.elements.

Any ideas?

+1  A: 

If there are nothing but form elements in it

$('#wrapper').children();

If there are going to be other things as well

$('#wrapper').children( 'input, select, textarea' );
Kerry
what about `<input name="elementA" />` that will not be found
jigfox
@Jens, that was the point exactly.
Tatu Ulmanen
He says "I would elementB and elementc but not elementA". In the beginning he asked for form elements within the container.
Kerry
Yes it is the second case. But I would prefer have no to do the list 'input, select, textarea' because I'm worried to not consider some form element type. Is there a way ti use form.elements array instead?
ungarida
@Kerry: you're right, sorry, I have missed this@ungarida: no, there is no easier way without a plugin
jigfox
A: 

jQuery('form[name=myform] div#wrapper').children();

sushil bharwani
do you want to find elementA as well. Try the code here it shouldnt be
sushil bharwani
+3  A: 

Use the :input pseudo selector if you don't want to specify them all:

$('#wrapper :input');

:input selects all input, textarea, select and button elements. And there's no need to use .children() here.

Tatu Ulmanen
I really need to look at more of the pseudo selectors. That's twice someone has come up with a better solution because I don't know them all :)
Kerry
Yes it is :), I do not know why I believed it was only for input elements...like :reset :radio...
ungarida
@ungarida: Because `input` and `:input` are only one character different, but the later includes all form control types.
R. Bemrose