views:

33337

answers:

8

I have a form with many input fields.

When I catch the submit form event with jQuery, is it possible to get all the input fields of that form in an associative array?

A: 

Associative? Not without some work, but you can use generic selectors:

var items = new Array();

$('#form_id:input').each(function (el) {
    items[el.name] = el;
});
Oli
actually, Lance's answer keeps the associative array intact for the POST values, and takes a whopping one line of code.
msumme
+36  A: 
$('#myForm').submit(function() {
    // get all the inputs into an array.
    var $inputs = $('#myForm :input');

    // not sure if you wanted this, but I thought I'd add it.
    // get an associative array of just the values.
    var values = {};
    $inputs.each(function() {
        values[this.name] = $(this).val();
    });

});

Thanks to the tip from Simon_Weaver, here is another way you could do it, using serializeArray:

var values = {};
$.each($('#myForm').serializeArray(), function(i, field) {
    values[field.name] = field.value;
});

Note that this snippet will fail on <select multiple> elements.

nickf
You are a master at this.
Vasil
http://api.jquery.com/serializeArray/ if you want to do this in one line :-)
Simon_Weaver
In newer versions of jQuery, Simon_Weaver's answer is more robust and easier.
MightyE
nickf
You're right nickf, the format of the data is different now that I look more closely at it. In fact, I'm surprised there isn't built-in support for this.
MightyE
+2  A: 
$('#myForm').bind('submit', function () {
  var elements = this.elements;
});

The elements variable will contain all the inputs, selects, textareas and fieldsets within the form.

David Dorward
+19  A: 

Late to the party on this question, but this even easier:

$('#myForm').submit(function() {
    // Get all the forms elements and their values in one step
    var values = $('#myForm').serialize();

});
Lance Rushing
This really is the best answer. It even keeps any array formatting you might have for your input fields. I don't think the answer by nickf would actually keep your data structures intact if it was formed using Zend_Form for example, where you'd have field[something] and field[something_else] as the names of the inputs... at least semantically, I can't see how it would...
msumme
According to the jQuery API docs, `.serialize()` (http://api.jquery.com/serialize/) puts all of the form's elements in a single string ready for appending to a URL in a query string, essentially mimicking a GET form request. This would not accomplish what nickf's answer accomplishes.
Christopher Parker
+3  A: 

The jquery.form plugin may help with what others are looking for that end up on this question. I'm not sure if it directly does what you want or not.

There is also the serializeArray function.

Simon_Weaver
+1 serializeArray is the best answer to this question
Andy
A: 

The top answer doesn't seem to take care of checkbox submission and will submit values even checkbox is not checked.

wsxedc
Not an answer - this should be a comment sir!
Andy
A: 

How to change when result onchange event?

Md Mazaharul
A: 

When I needed to do an ajax call with all the form fields, I had problems with the :input selector returning all checkboxes whether or not they were checked. I added a new selector to just get the submit-able form elements:

$.extend($.expr[':'],{
    submitable: function(a){
        if($(a).is(':checkbox:not(:checked)'))
        {
            return false;
        }
        else if($(a).is(':input'))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
});

usage:

$('#form_id :submitable');

I've not tested it with multiple select boxes yet though but It works for getting all the form fields in the way a standard submit would.

I used this when customising the product options on an OpenCart site to include checkboxes and text fields as well as the standard select box type.

slarti42uk