views:

24751

answers:

6

I want to loop over the elements of an HTML form, and store the values of the <input> fields in an object. The following code doesn't work, though:

function config() {
    $("#frmMain").children().map(function() {
        var child = $("this");
        if (child.is(":checkbox"))
            this[child.attr("name")] = child.attr("checked");
        if (child.is(":radio, checked"))
            this[child.attr("name")] = child.val();
        if (child.is(":text"))
            this[child.attr("name")] = child.val();
        return null;
    });

Neither does the following (inspired by jobscry's answer):

function config() {
    $("#frmMain").children().each(function() {
        var child = $("this");
        alert(child.length);
        if (child.is(":checkbox")) {
            this[child.attr("name")] = child.attr("checked");
        }
        if (child.is(":radio, checked"))
            this[child.attr("name")] = child.val();
        if (child.is(":text"))
            this[child.attr("name")] = child.val();
    });
}

The alert always shows that child.length == 0. Manually selecting the elements works:

    
>>> $("#frmMain").children()
Object length=42
>>> $("#frmMain").children().filter(":checkbox")
Object length=3

Any hints on how to do the loop correctly?

+2  A: 

jQuery has an excellent function for looping through a set of elements: .each()

$('#formId').children().each(
    function(){
        //access to form element via $(this)
    }
}
jobscry
Doesn't work for me, see my edit of the question. Further ideas?
Christian Aichinger
The form will likely have a paragraph or something similar as its child. You need to make sure you only select input elements.
SpoonMeiser
+11  A: 

don't think you need qutations on this:

var child = $("this");

try:

var child = $(this);
jobscry
Wow good catch - I didn't notice that
Hugoware
Thanks! It's working for me now.
Christian Aichinger
+5  A: 

Depending on what you need each child for (if you're looking to post it somewhere via AJAX) you can just do...

$("#formID").serialize()

It creates a string for you with all of the values automatically.

As for looping through objects, you can also do this.

$.each($("input, select, textarea"), function(i,v) {
    var theTag = v.tagName;
    var theElement = $(v);
    var theValue = theElement.val();
});
Hugoware
+1  A: 

I have used the following before:

var my_form = $('#form-id');
var data = {};

$('input:not([type=checkbox]), input[type=checkbox]:selected, select, textarea', my_form).each(
    function() {
        var name = $(this).attr('name');
        var val = $(this).val();

        if (!data.hasOwnProperty(name)) {
            data[name] = new Array;
        }

        data[name].push(val);
    }
);

This is just written from memory, so might contain mistakes, but this should make an object called data that contains the values for all your inputs.

Note that you have to deal with checkboxes in a special way, to avoid getting the values of unchecked checkboxes. The same is probably true of radio inputs.

Also note using arrays for storing the values, as for one input name, you might have values from several inputs (checkboxes in particular).

SpoonMeiser
Why are you writing $(this) twice....why not cache the lookup
redsquare
Did you vote me down for that? $(this) isn't a lookup, you're wrapping an existing object with a jQuery object. It might be slightly more efficient to only do this once, but the difference will be marginal.
SpoonMeiser
+1  A: 

if you want to use the each function, it should look like this:

$('#formId').children().each( 
  function(){
    //access to form element via $(this)
  }
);

Just switch out the closing curly bracket for a close paren. Thanks for pointing it out, jobscry, you saved me some time.

Peter
Why did you post this as an answer when you say you're just replying to `jobscry`? It's the same code he posted. That is what comments are for.
Shane Reustle
A: 

CAN I do a .descendants.each() ? I want nested elements as well.

toddv
Yes, you can :D
Cristian