> From: Mike Alsup
> 
> Seems to be faster rewriting it with a for loop instead of using each.
>  Matt, can you confirm?

Here's a skeleton of another way to approach the problem. Instead of
enumerating every element inside the form, it looks at each nodeName as it
goes and decides what to do from there.

This code is off the cuff and incomplete! :-)

$.fn.serialize = function() {
   var a = [];
   var taggers = {
      '#COMMENT':ignore, '#TEXT':ignore,
      INPUT:add, TEXTAREA:add, SELECT:select
   };
   
   this.each( function() {
      fields( this );
   });
   
   function fields( e ) {
      var tag = e.nodeName.toUpperCase();
      ( taggers[tag] || nest )( e );
   }
   
   function nest( e ) {
      for( e = e.firstChild;  e;  e = e.nextSibling ) {
         fields( e );
      }
   }
   
   function ignore( e ) {
   }
   
   function select( e ) {
      // not sure if you just want selectedIndex or go get the OPTION
      a.push({ name:e.name, value:e.selectedIndex });
   }
   
   function add( e ) {
      // some existing $.fn.serialize code goes here, ending with...
      a.push({ name:n, value:e.value });
   });

   return a;
};

-Mike


_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to