Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variables between functions in jQuery

I'm trying to make a simple on-focus clearer/on-blur restorer for forms on a site and I can't for the life of me figure out why this doesn't work.

$(document).ready(function() {

    $('.form-text').focus(function(keeper) {
        var keeper = $(this).attr('value');
        if($(this).val() == keeper) {
            $(this).val(''); 
        }
        return false;
    });

    $('.form-text').blur(function(keeper) {
        if($(this).val() == '') {
            keeper;
        }
        return false;
    }); 

 });

Any thoughts?

like image 733
t56k Avatar asked Dec 10 '22 03:12

t56k


1 Answers

Just make keeper in a outer scope, no need to be global. And var keeper = $(this).attr('value'); if($(this).val() == keeper) will always true, so that is not necessary.

$(document).ready(function() {
    var keeper;

    $('.form-text').focus(function() {
        keeper = $(this).val();
        $(this).val('');
        return false;
    });

    $('.form-text').blur(function() {
        if($(this).val() == '') {
            $(this).val(keeper);
        }
        return false;
    }); 
 });
like image 68
xdazz Avatar answered Dec 15 '22 00:12

xdazz