Question

If I have a large form and want to traverse only part of it (elements inside div#traverseMe), what would be the best solution to do this. Here is an example form:

<form id="form1">
 <input type="text" name="text1" />
 <div id="traverseMe">
  <input type="text" name="text2" />
  <input type="text" name="text3" />
 </div>
</form>

I would like to get all (and only) FORM elements that are inside traverseMe. I am currently using a javascript function that loops through all children (document.forms['form1'][i]) of the form with the goal of transforming the form into XML, which is fine for the entirety of the form but overkill considering I only need a portion of it. Is there a suitable jQuery solution for this?

Was it helpful?

Solution

Try -

$("#traverseMe > :input").each(function() {
  //do someting
})    

Demo - http://jsfiddle.net/ipr101/v3scP/1/

OTHER TIPS

You can use it's Id in the selector:

$('#traverseMe input').each(doStuff);

Or you can pass the context to the jQuery constructor:

var context = document.getElementById('traverseMe');
$('input', context).each(doStuff);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top