Andy Matthews wrote:
Is there a way to pass arguments from one anonymous function, (in
either Toggle, or Hover) to the second? Ideally I'd like to be able to
pass in whatever variables I choose, but I'd settle for being able to
pass in $(this), the item that triggered the event.
Here's the code:
// I have a set of links which show/hide items on a chart
$('a.display').toggle(function(){
var size = $(this).attr('href');
$('.' + size).hide();
},function(size ){ // can I pass "size" through to this second
function?
$('.' + size).show();
});
Anyone? If this is possible, then it needs to become part of the API
docs because I can't find it anywhere on the site.
Closures are your friend:
$('a.display').each(function() {
var $this = $(this), size = $this.attr('href'), toggle = $('.' + size);
$this.toggle(function() {
toggle.hide();
}, function() {
toggle.show();
});
});
HTH
--Klaus