Reputation: 67
Currently I'm trying to simulate a login / register form and I'm currently running in to a problem I can't seem to find an solution to where it says my "username" is undefined yet running the onReg
function first.
var tempusername
var temppassword
var tempconpassword
var username
var password
function onReg(tempusername, temppassword, tempconpassword, username, password){
tempusername = document.querySelectorAll("[name=username]")[0].value
temppassword = document.querySelectorAll("[name=password]")[0].value
tempconpassword = document.querySelectorAll("[name=conpassword]")[0].value
if(temppassword == tempconpassword && tempusername.length>2 && temppassword.length>2 && tempconpassword.length>2 ){
username = tempusername
password = tempconpassword
alert(username + " : " + password)
}
else if (password!=tempconpassword){
alert("Password doesnt match or is to short!")
}
}
function onLogin(username,password) {
alert("Does this work?" + username)
}
I think it might be because it's not a Global scope? and if so how could I come across doing it with this code?
Upvotes: 0
Views: 54
Reputation: 111
Do not use variables that have been declared already as parameters of a function. username
and password
are already declared as variables, so you should replace them with some other names. Try using usernameX
and passwordX
instead.
function onLogin(usernameX,passwordX) {
// will alert the value of the first parameter (usernameX)
alert("Does this work?" + usernameX)
//if you want to get the value of the variable
alert("Does this work?" + username)
}
Upvotes: 0
Reputation: 188
I think you should use document.getElementById("[name=username]").value
Upvotes: 0
Reputation: 10153
username
inside onLogin
is a formal function parameter. You have to explicitly pass a value to it when calling onLogin
.
var x = 'foo';
function checkX() {
alert(x);
}
function badCheckX(x) {
alert(x);
}
checkX(); // 'foo'
badCheckX(); // undefined
badCheckX('bar'); // 'bar'
badCheckX(x); // 'foo'
Upvotes: 1