Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterate through nested form elements in jquery

im sorry if this was posted already i have been looking to no avail..

I just want to know how to loop through nested form 'elements' (elements being not only the strict form elements like input tags but other html elements as well) in jquery. Currently i have this piece of code to do it:

$('#'+arguments[i].formid).children().each(function(){ 
    var child = $(this);
    alert(child.attr('id'));
    if(child.is(":input")) { alert(child.attr('id'));
     if(child.attr('id')!='') eval("p."+child.attr('id')+"='"+child.attr('value')+"'"); 
    }

       if(child.is(":textarea")) {
     if(child.attr('id')!='')  eval("p."+child.attr('id')+"='"+child.attr('value')+"'"); 
    }
   });

it does not work when my form contains other elements like this:

<form>
    <div id='tabs'>
        <ul>...</ul>
        <div id='tab-1'>
               <input type='text' id='fname' />
               <textarea id='desc' ></textarea>
        </div>
    </div>
</form>

please help...

like image 766
jan Avatar asked Dec 17 '22 05:12

jan


1 Answers

You can use contents() (and filter out text nodes if needed) or find('*') to get all elements, though I dislike the use of wildcards.

 $('form').contents()
          .filter( function() { return this.nodeType == 1; } )
          .each(...);

or

 $('form').find('*')
          .each(...);
like image 193
tvanfosson Avatar answered Dec 27 '22 04:12

tvanfosson