0

Say I have the following code:

var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            var responses = JSON.parse(this.responseText);
            var email = document.getElementById("email").value;
            var passwordIs = responses.email; // email should be replaced with the user's input value;
            document.write(passwordIs);
         }
    };
xmlhttp.open("GET","https://example.com", true);
xmlhttp.send();

How can I make it run such that when the user puts in an email, the email in the variable passwordIs gets replaced with the input?

To put it in simpler terms, when user enters jdoe@example.com, the variable passwordIs should "see" this: responses.jdoe@example.com Instead, it is "seeing" responses.email

1 Answers1

2

Use brackets instead of a dot:

var passwordIs = responses[email];

With dot notation, Javascript interprets the text after the dot as a literal, so you can't use a variable; see Dynamically access object property using variable