1
\$\begingroup\$
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.

\$\endgroup\$
  • \$\begingroup\$ Could you provide more context what data is about? \$\endgroup\$ – dfhwze Jun 26 at 18:53
  • \$\begingroup\$ @dfhwze api.jquery.com/Types/#PlainObject key-string value pairs. \$\endgroup\$ – diofloyk Jun 27 at 12:15
  • \$\begingroup\$ 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. \$\endgroup\$ – KIKO Software Jun 27 at 14:59
  • \$\begingroup\$ One thing I have noticed: The name of the function is fillFormInputs(), yet contrary to that name, it also creates an image. \$\endgroup\$ – KIKO Software Jun 27 at 15:01
  • \$\begingroup\$ @KIKOSoftware Ok. I'am edit my question. If i miss validation method, think it not will problem? \$\endgroup\$ – diofloyk Jun 27 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.