Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In JavaScript is there a way to make the variables global as you can do in python

//gets the username from the text box
function getUsername() {
let username = document.getElementById("usernameBox").value;
}
//alerts the user there username p.s. the site is still under development.
function sayUsername() {
alert(userename);
}

I keep getting the error message that the username variable does not exist, even though it is set in the function above. So my question is, how do I make the username variable set in both functions?

I have tried to put all the code above plus the other code for website in the same function, but that gives me errors. Then I have tried to make the username and password get put into an array but the values change when a different person logs in. So how do I make the variables work in all functions?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

let username

function getUsername() {
  username = document.getElementById("usernameBox").value;
}

function sayUsername() {
  alert(userename);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading