MHowey
MHowey

Reputation: 681

Setting a Variable in Jquery or Javascript

$('#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);
    }
});​

Upvotes: 1

Views: 103

Answers (4)

Gabriel Santos
Gabriel Santos

Reputation: 4974

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);
    }
});​

Upvotes: 0

jbabey
jbabey

Reputation: 46647

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);
    }
});​

Upvotes: 2

j08691
j08691

Reputation: 207901

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.

Upvotes: 1

Blacksad
Blacksad

Reputation: 15422

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");
    }
});​

Upvotes: 1

Related Questions