Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - get form elements by container id

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?

like image 882
Davide Ungari Avatar asked May 24 '10 17:05

Davide Ungari


2 Answers

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.

like image 175
Tatu Ulmanen Avatar answered Oct 23 '22 21:10

Tatu Ulmanen


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' );
like image 42
Kerry Jones Avatar answered Oct 23 '22 22:10

Kerry Jones