Search code examples
javascriptjquerydom-traversal

jquery traversing part of a form


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?


Solution

  • Try -

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

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