On Jan 8, 8:33 am, rich <dottedq...@gmail.com> wrote:
> Hello all, I'm relatively new to JavaScript.  I would like to loop
> through all the elements within a form and grab their values and what
> type of input they are.  So far I came up with:
>
> $(':input').each(function(idx, item) {
>      alert(idx);
> });

There are lots of form controls that aren't input elements. They are
all in the form's elements collection.


> The alert is placed there to see if it loops and it works as
> expected.  I'm not sure how to alert the input's value and what type
> it is.  Any help is greatly appreciated.

A form element's value is stored in its value property, its type is
stored in its type property. So:

var el,
    elements = document.forms[formName].elements;
for (var i=0, len=elements.length; i<len; i++) {
  el = elements[i];
  alert( el.type + ': ' + el.value);
}


--
Rob

Reply via email to