Maginot Junior wrote:
HI, I have a simples question, hope to get some answer...
Im having difficulty to pass vars to functions like: [ ... ]
$("#artigos_listagem li").mouseout(function(e){
$(this).css({
background: orgBg
});
});
Like you can see, inside the second function Im trying to use the orgBg
vars, and I would like to pass like .mouseout(function(e, otherVar){
You won't be able to do it that simply, as the mouseout won't call it
with your custom parameters. But you can add a closure. [1]
This is completely untested, but I think something like this would work:
$("#artigos_listagem li").mouseout((function(bkground) {
return function(e){
$(this).css({background: bkground});
})(orgBg);
});
This syntax creates and immediately executes an anonymous function that
returns a function in a closure that includes your val as param.
(function(param) {return function() {...})(val)
There is no real reason except for explanations like the above to use
the dummy variable bkground, so this is probably better once you
understand what's happening above:
$("#artigos_listagem li").mouseout((function(orgBg) {
return function(e){
$(this).css({background: orgBg});
})(orgBg);
});
Cheers,
-- Scott
[1] The first few references on Google are all pretty good:
http://www.google.com/search?q=javascript+closure