Discussion:
Passing value from function
Charlie22
2010-02-18 11:15:34 UTC
Permalink
Hi all,
I have trouble passing value as selector value to other function.

$(document).ready(function(){
var id = 'Test';
$('#Name').keyup(function(){
id = '#Mobil';
});

$(id).click(function(){
console.log(id);
});
});

I am getting on console value "#Mobil" but $(is) is using value
"Test". Thx for help in advance.

Charlie
RobG
2010-02-19 02:59:46 UTC
Permalink
Post by Charlie22
Hi all,
I have trouble passing value as selector value to other function.
$(document).ready(function(){
        var id = 'Test';
        $('#Name').keyup(function(){
                id = '#Mobil';
If you want variables to be local to their enclosing function, declare
them with var.

The variable id has a closure to id in the outer function. Before the
keup runs, id is 'Test'. Afterward, its value is "#Mobil".
Post by Charlie22
        });
        $(id).click(function(){
I guess that should be:

$('#' + id).click(function(){
Post by Charlie22
        console.log(id);
This id also has a closure to the outer id.
Post by Charlie22
        });
});
I am getting on console value "#Mobil" but $(is) is using value
"Test". Thx for help in advance.
All the id variables above refer to the same property of the same
activation object (i.e. they are essentially the same variable).
Modifying any one of them modifies the others.


--
Rob

Loading...