Need help with my slidetoggle function

Need help with my slidetoggle function

I currently have a slidetoggle function which is working great!  What my next goal is, is to create a link inside the div that i'm slide toggling, that will close the parent div that it is in.

Here my code so far...

  1. /*JQ - EXPAND PANELS ON CLICK*/
  2. $('#panels .expand').click(function(e){
  3. e.preventDefault();
  4. var mclass=$(this).attr('class').split(' ')[1];
  5. var $mcontent = $("#" + mclass);
  6. $mcontent.slideToggle('slow', function(){
  7. mthis = $(this);
  8. if (mthis.is(':hidden')) {
  9. mthis.prev().css({'background-image':'url("images/arrow-down.png")'});
  10. }
  11. else {
  12. mthis.prev().css({'background-image':'url("images/arrow-up.png")'});

  13. var menulink = $(this).children().children().next().next('a:last').attr('class');
  14. //console.log('where am i? '+ menulink);
  15. $('.'+ menulink).click(function(e) {
  16. e.preventDefault();
  17. $mcontent.toggle();
  18. mthis.prev().css({'background-image':'url("images/arrow-down.png")'});
  19. });
  20. }
  21. });
  22. });

If you look at lines 16 - 20, you will see that I'm trying to click on an internal link to have the same effect as $mcontent on line 8 - basically to slidetoggle where I am at.  But it's acting buggy when I click that link (the link associated to lines 16 - 20 in code), (sometimes it closes the div, other times it doesn't).

How can I fix this so that a children() anchor link can bind to the slidetoggle parent function()?
So matter if I am clicking on  $('#panels .expand')   or the internal anchor link, it will slidetoggle the entire function properly.

Thanks!


*Break through the ice and never look back*