I am trying to make a generic form parser for Javascript. The idea being, a developer could drop the function onto a form and sumbit via AJAX, or do something with a form besides submit it. The result will be in the same format used in a query string.
What I have posted works, so I am basically wondering if this is a good design? What could be improved? Thanks for the help!
function test_submit(id){
var results=[],form=document.getElementById(id);
for(var i=0;i<form.elements.length;++i){
var obj=form.elements[i];
if(obj.name&&!obj.disabled){
switch(obj.tagName){
case 'SELECT':
for(var j=0;j<obj.length;++j){
if(obj.options[j].selected){
var value=obj.options[j].value;
if(!value)value=obj.options[j].text;
results.push(obj.name+'='+escape(value));
}
}
break;
case 'INPUT':
case 'TEXTAREA':
var type=obj.type,value=obj.value;
if(type)type=type.toLowerCase();
if(type&&(type=='radio'||type=='checkbox')){
if(obj.checked)results.push(obj.name+'='+(value?escape(value):'on'));
}
else results.push(obj.name+'='+escape(value));
break;
}
}
}
return results.join('&');
}