$.fn.values = function (data) {
var els = $(this).find(':input').get();
if (typeof data != 'object') {
// return all data
data = {};
$.each(els, function () {
if (this.name && !this.disabled && (this.checked || /select|textarea/i.test(this.nodeName) || /text|hidden|password/i.test(this.type))) {
data[this.name] = $(this).val();
}
});
return data;
} else {
$.each(els, function () {
if (this.name && data[this.name]) {
if (this.type == 'checkbox' || this.type == 'radio') {
$(this).attr("checked", (data[this.name] == $(this).val()));
} else {
$(this).val(data[this.name]);
}
}
});
return $(this);
}
};
$("#form1").submit(function () { //map both forms field, form1 field as key & form 2 field as value
var mapper = {
"name1": "name2", //name1 from form1 corrosponds to name2 from form2
"address1": "address2", //address1 from form1 corrosponds to address2 from form2
"postcode1": "postcode2" //postcode1 from form1 corrosponds to postcode2 from form2
};
var formData1 = $(this).values();
var formData2 = {};
//filling data2 from data 1
for (var i in mapper) {
formData2[mapper[i]] = formData1[i];
}
$("#form2").values(formData2); //filling form 2 from data2
return false;
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.