views:

28

answers:

1

Hi - I'm making a slider and this is the meat of my code;

$('#slider a').click(function () {
                elem.filter(this.hash).addClass("active").animate({"right":"0"}, 3000, 
                    function() {
                        elem.filter(activeElem).removeClass("active");
                        activeElem = elem.filter(this.hash);
                    });

I'm trying to remove the class "active" from the existing viewable element, then adding "active" to the new element. Yet, when I run it, I get a an error in Firebug sayed, "b is undefined" and in IE that "nodeType is null or not an object".

Can you not set variables in a callback function?

A: 

Since you're storing activeElem as a jQuery object anyway, you can simplify your code and remove the probably offending .filter() check, like this:

$('#slider a').click(function () {
   $(this).addClass("active").animate({"right":"0"}, 3000, function() {
      activeElem.removeClass("active");
      activeElem = $(this);
   });
});

I can't say for sure what your error on the filter check is currently without seeing how you're initializing the collections, but since you're storing it as a jQuery element anyway, and .hash denotes an #ID selector the above should be a much simpler solution...or if there's only one .active class element, just do this:

$('#slider a').click(function () {
   $(this).addClass("active").animate({"right":"0"}, 3000, function() {
      $(".active").not(this).removeClass("active");
   });
});
Nick Craver