stevenspiel
stevenspiel

Reputation: 5999

Global variable passed between functions jQuery

I am setting a variable in an if...else statement. However, when I try to call the variable in another function, I get the error that the variable is not defined. How can I set a global variable?

function username_check(){  
username = $('#username').val();
if(username == "" || username.length < 7 || username.indexOf(' ') > -1){
    usernameFilled = 0;
else{
    usernameFilled = 1;
}   
}

function email_check(){ 
email = $('#email').val();
if(email == "" || email.indexOf(' ') > -1) {
    emailFilled = 0;
}else{
    emailFilled = 1;
}
}

function password_length(){ 
password = $('#password').val();
if(password == "" || password.indexOf(' ') > -1) {
    passwordFilled = 0;
}else{
    passwordFilled = 1;
}
}

function password_check(){  
  password2 = $('#password2').val();
  password = $('#password').val();
  if(password2.length > 7 && password2 == password) {
    password2Filled = 1; /**setting the variable**/
  }else{
    password2Filled = 0;
  }
}

function upload(){
if (usernameFilled == 0 || emailFilled == 0 || passwordFilled == 0 || password2Filled == 0) {
    alert('All fields are required');
}else{
    /**upload to database**/
}

Upvotes: 0

Views: 4334

Answers (3)

Alex W
Alex W

Reputation: 38173

You create global variables in JavaScript by defining them outside of the scope of all functions. Like so:

<script type="text/javascript">
var globalVariable;


function blah()
{
    globalVariable = "something";
}

function anotherFunction()
{
    alert(globalVariable);
}
</script>

The ECMAScript / JavaScript documentation states that a "Global Object" should be created outside of any execution context.

Upvotes: 0

adeneo
adeneo

Reputation: 318182

You're probably calling the functions in the wrong order, or doing something else wrong, cause it works just fine for me, edited the code slightly to test it:

function password_check(){  
  var password2 = $('#password2').val(), 
      password = $('#password').val();
  password2Filled = (password2.length > 7 && password2 == password) ? 1:0;
}

function upload(){
    console.log(password2Filled); //prints 0 or 1 just fine ???
}

$("#btn").on('click', function() {
    password_check();
    upload();
});

FIDDLE

Upvotes: 1

Ben
Ben

Reputation: 11471

Rather than setting a global variable just return password2Filled and save it outside the function. Then you can pass it into the next function.

ie

function password_check(){  
   password2 = $('#password2').val();
   password = $('#password').val();
   if(password2.length > 7 && password2 == password) {
       password2Filled = 1; /**setting the variable**/
   }else{
       password2Filled = 0;
   }
   return password2Filled;
}

function upload(password2Filled)
{
    if (password2Filled == 0) { /**calling the variable**/
        alert('All fields are required');
    }else{
        /**upload to database**/
    }
}

....

var passwordsOk = password_check();
upload(passwordOk);

Try and avoid global variables, they clutter up the program and make it difficult to see the flow of the code and to create code that is reusable.

Upvotes: 1

Related Questions