I have an HTML form and using Serialization jQuery snippet to convert form data to JSON representation as taught in this: Convert form data to JavaScript object with jQuery
I am only able to display the JSON output in an div tag. But I want to display the output in a new file or window upon form submit button click.
My jQuery Serialization snippet in header:
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
$(function() {
$('form').submit(function() {
$('#result').text(JSON.stringify($('form').serializeObject(), null, 4));
return false;
});
})
My HTML
<form id="student" method="post">
<label>Name: </label><input type="text" name="studentname" /><br />
<label>Age: </label><input type="text" name="age" /><br />
<label>City: </label><input type="text" name="city" />
<input type="submit" value="Submit" id="submitbutton" /><br />
</form>
<pre id="result"></pre>