I want to get all the data from my form and put it into an accessible array. Here’s what I have so far:
var data = $("#everything").serializeArray();
var test = [];
$(data).each(function(index, element) {
test[element] = element.val();
});
I tried this:
alert(data["fname"]);
But that returned “undefined” even though there was something in the textbox, which is why i have to do that .each method. But now when I do
alert(test["fname"]);
It doesn’t even pop up the message. Does anyone know what I’m doing wrong? I just want to put all the form data into an array that I can access by id or name of the inputs.
A few things about your code… First
testis an array but you’re using it as an object liketest[element], this will cause trouble. ThenserializeArraycreates an array of objects, each object containsnameandvalueso there’s noelement.val(), plus I’m pretty sureelementis the DOM element so you’d have to either wrap it in jQuery like$(element).val()or use the native property likeelement.valuebut again, this won’t get you the value you’re looking for. Try this:Demo: http://jsfiddle.net/elclanrs/KRbPg/