функция копирует значение в js не работает значение "undefined" - PullRequest
0 голосов
/ 14 июля 2020

Мне нужно скопировать значение ввода с идентификатором "test-id" в 2 других поля ввода. Когда я использую это:

// start copy  implementation   
function copyTextValue() {

if(document.getElementById('accetta-condizioni').checked){
        let text1 = document.getElementById('billing_phone').value;        
        document.getElementById('reg_username').value = text1;
        document.getElementById('reg_password').value = text1;
    }
    else{
        document.getElementById('reg_username').value = "";
        document.getElementById('reg_password').value = "";
    }    
 }

в этом html

<input type="tel" class="input-text" name="billing_phone" id="test-id" value="" placeholder="Number...">
<input type="checkbox" id="acpt-id" name="accetta-condizioni" onclick="copyTextValue();">

<input type="text" id="reg_username" autocomplete="username" value="">
<input type="password" id="reg_password" autocomplete="new-password">

результат "undefinied" в значении fiedls. Почему?

1 Ответ

0 голосов
/ 14 июля 2020

Проблема решена, код:

function copyTextValue() {

if(document.getElementById('accetta-condizioni').checked){
    
    var inputs = document.getElementsByTagName('input');

for(var i = 0; i < inputs.length; i++) {
    if(inputs[i].type.toLowerCase() == 'tel') {
       // alert(inputs[i].value);
         document.getElementById('reg_username').value = inputs[i].value;
        document.getElementById('reg_password').value = inputs[i].value;
            
    }
}
       
    }
    else{
        document.getElementById('reg_username').value = "";
        document.getElementById('reg_password').value = "";
    }    
 }
   
...