-2

I have some variables that I want to use in jQuery's focus event. How do I declare them so they are in the focus callback?

var height = 72;

$('#myfield').focus(function() {
    heightinfeet = height / 12;
    alert(heightinfeet);
});

For me, height is always 0. There are several variables to pass and I've tried making it an object using this for properties and that didn't pass them in either

/* real code */

function GOPRO_pfFieldConfirmation(oField1, oField2, oMarkerField, bCaseSens) {

var GOPRO_iFieldLen1 = $(oField1).val().length;
var GOPRO_iFieldLen2 = $(oField2).val().length;
var GOPRO_sFieldVal1 = $(oField1).val();
var GOPRO_sFieldVal2 = $(oField2).val();
var GOPRO_sFieldSub = GOPRO_sFieldVal1.substring(0, GOPRO_iFieldLen2);

//if not case sensitive, make all upper case

if (bCaseSens == false) {
    GOPRO_sFieldVal1 = GOPRO_sFieldVal1.toUpperCase();
    GOPRO_sFieldVal2 = GOPRO_sFieldVal2.toUpperCase();
    GOPRO_sFieldSub = GOPRO_sFieldSub.toUpperCase();

}

$(oField1).focus( function() {
    console.log(GOPRO_iFieldLen1);
    if ((GOPRO_sFieldVal1 == GOPRO_sFieldVal2) && GOPRO_iFieldLen1 > 0) {
        GreenMarker();
    }
    else if (GOPRO_iFieldLen1 > 0 && GOPRO_iFieldLen2 > 0) {
        RedMarker();
    }
    else {
        NoMarker();
    }

});

...

    GOPRO_pfFieldConfirmation($('#reg_email'), $('#reg_email2'), $('.field_confirm:eq(3)'), false);

console.log is always 0, actually all the predefined variables return 0 or null strings

The Hawk
  • 1,496
  • 6
  • 26
  • 43

1 Answers1

0

This isn't a scope problem, here is what is happening:

  1. You call this function which selects your elements and stores the length of the value within the input box
  2. Later a person focuses into the box while it is empty, and 0 is logged since that what was stored earlier
  3. They type in the box, blur away and focus in again; this time you still get 0 because that is still what was stored on the initial call to the function, that variable was never updated.

You need to do something more like this:

function GOPRO_pfFieldConfirmation(oField1, oField2, oMarkerField, bCaseSens) {
    var $field1 = $(oField1), $field2 = $(oField2);

    $field1.focus(function() {
        //put these in the focus event, so they are bound everytime.
        var GOPRO_sFieldVal1 = $field1.val();
        var GOPRO_sFieldVal2 = $field2.val();
        var GOPRO_iFieldLen1 = GOPRO_sFieldVal1.length;
        var GOPRO_iFieldLen2 = GOPRO_sFieldVal2.length;
        var GOPRO_sFieldSub = GOPRO_sFieldVal1.substring(0, GOPRO_iFieldLen2);
        //if not case sensitive, make all upper case  
        if (bCaseSens == false) {
            GOPRO_sFieldVal1 = GOPRO_sFieldVal1.toUpperCase();
            GOPRO_sFieldVal2 = GOPRO_sFieldVal2.toUpperCase();
            GOPRO_sFieldSub = GOPRO_sFieldSub.toUpperCase();
        }

        console.log(GOPRO_iFieldLen1);
        if ((GOPRO_sFieldVal1 == GOPRO_sFieldVal2) && GOPRO_iFieldLen1 > 0) {
            //GreenMarker();
        } else if (GOPRO_iFieldLen1 > 0 && GOPRO_iFieldLen2 > 0) {
            //RedMarker();
        } else {
            //NoMarker();
        }
    });
}

GOPRO_pfFieldConfirmation('#reg_email', '#reg_email2', '.field_confirm:eq(3)', false);

I added a couple other things like: caching the jquery object so you create it only once, making less calls to .val(), and not wrapping an object twice (you made a jquery object in the function call, then wrapped it again inside).

You can see a jsFiddle of it working. It will still be 0 when you first focus into the box, but focusing into a box with text will yield the expected results.

Chad
  • 19,219
  • 4
  • 50
  • 73