I am iterating through html element which is input field by using jquery, but I get error. Here is my code:
$(".submit_button").on("click touch", function(e){
e.preventDefault();
var formdata = {};
$(this).closest("div")
.find("[data-field]")
.each(function(i, val){
var field = $(this).attr("data-field");
if($(this).is("[data-group]")) {
var group = $(this).attr("data-role");
formdata[group][field] = $(this).val() || "";
} else {
formdata[field] = $(this).val() || "";
}
});
alert(JSON.stringify(formdata));
return;
});
When I click submit button, the error console in firebug said that:
formdata[group] is undefined
How should it be written, so it is run correctly?
{}). It's not really being used as a JavaScript array.