# Fills inputs of form

function fillFormInputs(data) {
$('input[name="name"]').val(data.name);$('input[name="surname"]').val(data.surname);
$('input[name="patronymic"]').val(data.patronymic);$('input[name="email"]').val(data.email);
$('input[name="skype"]').val(data.skype);$('input[name="icq"]').val(data.icq);
$('input[name="homeAddress"]').val(data.homeAddress);$('input[name="additionalInfo"]').val(data.additionalInfo);
$('input[name="birthday"]').val(dateInISOFormat(new Date(data.birthday))); for (var i = 0; i < data.phones.length; i++) { var phone = data.phones[i]; insertPhoneNumber(phone.number, phone.type); }$("img:first").attr("src", "data:image/png;base64, " + data.image.content);
}

function dateInISOFormat(date) {
var month = date.getMonth() + 1;
var day = date.getDate();
var year = date.getFullYear();
return year + "-" + month + "-" + day;
}

function insertPhoneNumber(number, type) {
if (checkValidPhone(number)) {
document.getElementById("incorrectNumber").hidden = true;
insertRow(number, type);
} else {
document.getElementById("incorrectNumber").hidden = false;
}
}

var phoneCount = document.getElementById("phoneTable").getElementsByTagName("tr").length;

function insertRow(number, type) {
var phoneTable = document.getElementById("phoneTable");
var row = phoneTable.insertRow(0);
insertInput(row.insertCell(0), "number", number);
insertInput(row.insertCell(1), "type", type);
var tdForDelBtn = row.insertCell(2);
var deletePhoneBtn = document.createElement("button");
deletePhoneBtn.setAttribute("name", "deletePhone");
deletePhoneBtn.setAttribute("onclick", "deleteRow(this)");
deletePhoneBtn.innerHTML = "delete";
tdForDelBtn.appendChild(deletePhoneBtn);
phoneCount++;
}


data it is planeObject.

Method works fine, but can I rewrite this simpler? data contains pairs key - value, where value string. Most interesting for me fillFormInputs(data) method, but i ready look at other problems in my code. Thanks.

• Could you provide more context what data is about? – dfhwze Jun 26 at 18:53
• @dfhwze api.jquery.com/Types/#PlainObject key-string value pairs. – diofloyk Jun 27 at 12:15
• We cannot refactor it because not all the code is there; dateInISOFormat() and insertPhoneNumber() are missing. The latter is important, because it, presumably, also fills something. Apart from that, what's the point of refactoring such a tiny piece of code? I would like to see how, and why, you ended up with such a weird \$data structure. – KIKO Software Jun 27 at 14:59
• One thing I have noticed: The name of the function is fillFormInputs(), yet contrary to that name, it also creates an image. – KIKO Software Jun 27 at 15:01
• @KIKOSoftware Ok. I'am edit my question. If i miss validation method, think it not will problem? – diofloyk Jun 27 at 16:49