I have this question, there are 2 fields that need to be linked, leave the code:

<span>Номер отделения</span> <input type="text" required size="90px" id="departament" oninput="input()"> <span>Адрес отделения</span> <input type="text" required size="90px" id="address" disabled> 

the task is, a person writes in the field "branch number", for example, branch number 1, the address of this branch is entered in the "address of branch" field immediately. that is, let's say there are 5 branches, and each has its own address, the "branch address" field is not editable, the address is automatically driven in, I tried to do with oninput, but something did not go in, it was said that the branch number and address is taken from the array , and substituted, tell me the solutions, perhaps through a cycle of how to substitute, thanks

  var dep_address=new Array(); dep_address[0]=1; dep_address[1]="address 1 departament"; dep_address[2]=2; dep_address[3]="address 2 departament"; dep_address[4]=3; dep_address[5]="address 3 departament"; dep_address[6]=4; dep_address[7]="address 4 departament"; dep_address[8]=5; dep_address[9]="address 5 departament"; 

    1 answer 1

     function inputDep(el) { var dep_address = [ { number: 1, address: "address 1 departament" }, { number: 2, address: "address 2 departament" }, { number: 3, address: "address 3 departament" }, { number: 4, address: "address 4 departament" }, { number: 5, address: "address 5 departament" }, { number: 6, address: "address 6 departament" }, ]; var item = dep_address.find(function(i) { return i.number == el.value; }); if (item) document.getElementById("address").value = item.address; } 
     <span>Номер отделения</span> <input type="text" required size="90px" id="departament" oninput="inputDep(this)"> <span>Адрес отделения</span> <input type="text" required size="90px" id="address" disabled> 


    • dep_adress is an array of objects

    Why, then, in this array, the number variable has a value not through "=" but through ":"

    • it worked, thank you so much, but I don’t know many more things in JS, could you read the code that you wrote would be very grateful - Roman Romashko
    • @RomanRomashko I think it will be more correct if you say that you don’t understand the answer code. - Igor
    • The variable dep_adress is an object of arrays, if I understand correctly) but why then in this array, the number variable has a value not through "=" but through ":" and adress itself. and the second question is what does find here dep_address.find mean? - Roman Romashko
    • I understand, thank you, I will continue to understand - Roman Romashko