简体   繁体   中英

Set Input Value to Javascript Variable Error

Using JavaScript, I want to copy the values from Shipping Name and Shipping Zip to the Billing Name and Billing Zip if the check box is selected.

When I execute the following code, it shows [object HTMLInputElement] in both Billing Name and Billing Zip fields.

function billingFunction(){
    if (document.getElementById('same').checked){
        var name = document.getElementById('shippingName');
        var zip = document.getElementById('shippingZip');
        document.getElementById('billingName').value = name;
        document.getElementById('billingZip').value = zip;
    }

    else{
        document.getElementById('billingName').value = ""
        document.getElementById('billingZip').value = ""
    }

}

Add value after variables like below code:

function billingFunction(){
    if (document.getElementById('same').checked){
        var name = document.getElementById('shippingName');
        var zip = document.getElementById('shippingZip');
        document.getElementById('billingName').value = name.value;
        document.getElementById('billingZip').value = zip.value;
    }

    else{
        document.getElementById('billingName').value = ""
        document.getElementById('billingZip').value = ""
    }

}

you are assigning the objects your variables but not the value

 function billingFunction(){ if (document.getElementById('same').checked){ var name = document.getElementById('shippingName'); var zip = document.getElementById('shippingZip'); name=name.value; zip=zip.value; document.getElementById('billingName').value = name; document.getElementById('billingZip').value = zip; } else{ document.getElementById('billingName').value = "" document.getElementById('billingZip').value = "" } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM