tags:

views:

76

answers:

4

I need tp serialize a group of input elements but I can't for the life of me figure out this simple task.

I can successfully iterate through the targeted inputs using:

$("#tr_Features :input").each(function() {
    ...
}

Here's my code, that doesn't work:

var features = "";
$("#tr_Features :input").each(function() {
    features += {$(this).attr("name"): $(this).val()};
}

Serializing the entire form won't give me what I need. The form has much more than this subset of inputs. This seems like it should be a pretty straightforward task but apparently programming late into a Friday afternoon isn't a good thing.

If it's helpful, here's the form inputs I'm targeting:

<table cellspacing="0" border="0" id="TblGrid_list" class="EditTable" cellpading="0">
<tbody><tr class="FormData" rowpos="1">
    <td class="CaptionTD ui-widget-content">Cable Family</td>
    <td class="DataTD ui-widget-content" style="white-space: pre;">&nbsp;<input type="text" value="" id="feature_id:8" name="feature_id:8"></td>
</tr>
<tr class="FormData" rowpos="1">
    <td class="CaptionTD ui-widget-content">Material</td>
    <td class="DataTD ui-widget-content" style="white-space: pre;">&nbsp;<input type="text" value="" id="feature_id:9" name="feature_id:9"></td>
</tr>
<tr class="FormData" rowpos="1">
    <td class="CaptionTD ui-widget-content">Thread Size</td>
    <td class="DataTD ui-widget-content" style="white-space: pre;">&nbsp;<input type="text" value="" id="feature_id:10" name="feature_id:10"></td>
</tr>
<tr class="FormData" rowpos="1">
    <td class="CaptionTD ui-widget-content">Attachment Style</td>
    <td class="DataTD ui-widget-content" style="white-space: pre;">&nbsp;<input type="text" value="" id="feature_id:11" name="feature_id:11"></td>
</tr>
<tr class="FormData" rowpos="1">
    <td class="CaptionTD ui-widget-content">Feature</td>
    <td class="DataTD ui-widget-content" style="white-space: pre;">&nbsp;<input type="text" value="" id="feature_id:12" name="feature_id:12"></td>
</tr>
<tr class="FormData" rowpos="1">
    <td class="CaptionTD ui-widget-content">Comments</td>
    <td class="DataTD ui-widget-content" style="white-space: pre;">&nbsp;<input type="text" value="" id="feature_id:13" name="feature_id:13"></td>
</tr>

+2  A: 

you're trying to += an array... try using features.push or use the index from the $.each function: features[i] = someval

EDIT

I noticed that you have the same id for all your trs (and some of your tds!). That may be causing some problems as well. Make sure all your ids are unique.

Jason
Good catch on the duplicate IDs. That was an oversight and has been corrected. The above code was a typo too. Man this Friday feels like a Monday. Thanks.
gurun8
sounds like someone has a case of the fridays :(
Gabriel
@Jason: Given new edits this answer doesn't really apply any more.
R0MANARMY
+1  A: 

+= doesn't add objects to arrays in JavaScript. What you want is .push():

var features = new Array();
$("#tr_Features :input").each(function(){
    features.push({$(this).attr("name"): $(this).val()});
});

But wait, there's more! jQuery doesn't support converting an object to a JSON string as far as I know (it supports parsing JSON, though). You need to use a separate JSON library for that, such as this one.

Joey Adams
what would `features.join(',');` do?
Jason
It would yield `"[object Object],[object Object],[object Object],[object Object],..."`
Joey Adams
yeah, figured. just checking
Jason
Maybe its a case of the it's gorgeous out Fridays, but I don't see any encoding examples at json.org. At least in PHP I can use json_encode(). Shouldn't there be something similar in jquery or json2.js?
gurun8
+2  A: 

EDIT:

If I'm understanding your question correctly, this will give you the json object you want.

Note that it requires the json2 library.

var features = {};    // Create empty javascript object

$("#tr_Features :input").each(function() {           // Iterate over inputs
    features[$(this).attr('name')] = $(this).val();  // Add each to features object
});

var json = JSON.stringify(features); // Stringify to create json object (requires json2 library)

Thanks to R0MANARMY for pointing out the intended json format in the question.


Is there a reason you don't use jQuery's .serializeArray() function? You can run it on a subset of elements instead of the entire form.

$("#tr_Features :input").serializeArray();

From the docs: http://api.jquery.com/serializeArray/

The .serializeArray() method creates a JavaScript array of objects, ready to be encoded as a JSON string.

The .serializeArray() method can act on a jQuery object that has selected individual form elements, such as <input>, <textarea>, and <select>.

patrick dw
That's pretty slick. It wouldn't work for the question because `serializeArray` returns objects in the format of `{ name: <name>, value: <value> }` where as the question asks for them to be in the form of `{ <name>: <value> }`.
R0MANARMY
@R0MANARMY - Yes, I see what you mean. Thank you for the correction.
patrick dw
+1  A: 

Apparently all I was creating was an object the long and hard way. That sounded worse that it should have but it's true. All I needed to was use the name attribute at the index/key and the val() as the value.

var data = new Object();
$("#tr_Features :input").each(function() {
    data[$(this).attr("name")] = $(this).val();
});
return data;

This worked. Who knew?! Simple really. Y'all got an up arrow from me for lending a hand and going on this escalate I call Friday. Cheers!

gurun8
I just updated my answer, but I see you got it figured out. If you want an actual JSON object, you'll still need to stringify it. You can see how in my answer (super easy).
patrick dw
Just noticed you're using an Array instead of an Object. Is there a reason for that. I think it is generally not recommended to use an Array as a hash. http://ajaxian.com/archives/javascript-associative-arrays-considered-harmful
patrick dw
I guess there are two reasons. One is worked and two a hash is the only way I know creating dynamic object properties. If you know a better way, I'm open to suggestions. The link you provided seems more concerned with Prototype than anything else.
gurun8
@gurun8 - Yeah, Prototype is a big reason, which won't apply here. Perhaps this is a better article. http://andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/ If for no other reason, it is not a typical use of an Array in programming. Not sure why js lets you do it. As far as how to do it, see my answer. Just create an object instead of an array. Everything else stays the same.
patrick dw
Great point. I knew that JS object can be handled as associative arrays but for some reason, the two types occupied that same space in my brain. I (obviously) didn't realize they were different and behaved differently. Thanks for clarification and alerting me to best practice. Answer above changed to reflect recommendation.
gurun8
+1 - Looks good!
patrick dw