1
$('#firstname').bind('focusout focusin', function() {
    var get_firstname = $("#firstname").val().replace(/ /g, "+");
    var n_firstname = get_firstname.length;
    if (n_firstname > 2) {
        $(".firstname_good").fadeIn("fast");
        var firstname_v = 1;
    }
    else {
        $(".firstname_good").fadeOut("fast");
        var firstname_v = '';
    }
});​

I'm have trouble understanding how to use the VAR firstname_v below. What do I need to do in the above to be able to use it below

$('#lastname').bind('focusout focusin', function() {
    if (firstname_v == 1) {
        alert(firstname_v);
    }
});​
5
  • You need to make it global or put it outside of functions Commented Jul 13, 2012 at 18:50
  • Research variable scope. Not understanding variable scope can lead to serious code problems later. Commented Jul 13, 2012 at 18:53
  • I don't have a strong understanding of variable scope or Jquery for that matter. That's why I came here with my question. I wanted to hear what the pro's had to say :) Commented Jul 13, 2012 at 19:00
  • I would post a link to a tutorial, but there are way too many to choose from. Commented Jul 13, 2012 at 19:02
  • Thanks anyway Kevin. I read through several already today. I've been writing php for years but I'm still a newbie when it comes to javascript and jquery Commented Jul 13, 2012 at 19:04

4 Answers 4

2

You need to declare the variable in a higher scope that both inner scopes have access to:

var firstname_v; // declare it

$('#firstname').bind('focusout focusin', function() {
    var get_firstname = $("#firstname").val().replace(/ /g, "+");
    var n_firstname = get_firstname.length;
    if (n_firstname > 2) {
        $(".firstname_good").fadeIn("fast");
        firstname_v = 1; // no var here, you're just setting it
    }
    else {
        $(".firstname_good").fadeOut("fast");
        firstname_v = ''; // no var here, you're just setting it
    }
});​

$('#lastname').bind('focusout focusin', function() {
    if (firstname_v == 1) { // no var here, you're just getting it
        alert(firstname_v);
    }
});​
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is your variable is local to the callback function.

Declare it outside like this:

   var firstname_v = '';

   $('#firstname').bind('focusout focusin', function() {
    var get_firstname = $("#firstname").val().replace(/ /g, "+");
    var n_firstname = get_firstname.length;
    if (n_firstname > 2) {
        $(".firstname_good").fadeIn("fast");
        firstname_v = 1;
    }
    else {
        $(".firstname_good").fadeOut("fast");
    }
});​

Comments

1

Change the top block to:

var firstname_v;
$('#firstname').bind('focusout focusin', function() {
    var get_firstname = $("#firstname").val().replace(/ /g, "+");
    var n_firstname = get_firstname.length;
    if (n_firstname > 2) {
        $(".firstname_good").fadeIn("fast");
        firstname_v = 1;
    }
    else {
        $(".firstname_good").fadeOut("fast");
        firstname_v = '';
    }
});​

Note that the only real change is that the declaration of firstname_v is now done outside the function rather than inside. This was it will be available throughout your code.

2 Comments

varfirstname_v; should be var firstname_v;
@GabrielSantos - Yup, typo fixed.
0

Do this:

var firstname_v = 0;

$('#firstname').bind('focusout focusin', function() {
    var get_firstname = $("#firstname").val().replace(/ /g,"+");
    var n_firstname = get_firstname.length;

    if (n_firstname > 2) {  
        $(".firstname_good").fadeIn("fast"); 
        firstname_v = 1; 
    } else { 
        $(".firstname_good").fadeOut("fast"); 
    }
});

$('#lastname').bind('focusout focusin', function() {
    if (firstname_v == 1) {
        alert(firstname_v);
    }
});​

3 Comments

i don't think this addresses the issue.
you dont need to try to see that firstname_v would be undefined in the other event handler you didn't post.
You are right. I have copied only the top of code. Forgot the second bind

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.