简体   繁体   中英

How can I pass a variable from one function to another?

I have this code snippet:

function pWordValidate() {
    var pWord = document.getElementById("password");
    if (pWord.value.length < 6 || pWord.value.length > 20) {
        errorList("password must be between 6 - 20 length");
    }
    pWordValidate2(pWord);
}

function pWordValidate2(pWord) {
    var pWord2 = document.getElementById("retypepassword");
    if (pWord2 != pWord) {
        errorList("passwords do not match");
    }
    showErrors();
}

When I try to output "pWord" just above the line calling the 2nd function I get an output of "objectHTMLInputElement"

The line

if (pWord2 != pWord) {

should really be

if (pWord2.value != pWord.value) {

in fact both pWord and pWord2 are DOM objects for which you need to compare the 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