tags:

views:

36

answers:

3

Hi I have this code:

$(document).ready(function () {
        $(".LeftColumn").hide();
        $(".SidebarToggle").toggle(function () {
            $(this).addClass("active");
            $(this).text("Hide Sidebar");
            $(this).attr("title", "Hide Sidebar");
            $(".LeftColumn").fadeIn("fast");
            return false;
        },
        function () {
            $(this).removeClass("active");
            $(this).text("Show Sidebar");
            $(this).attr("title", "Show Sidebar");
            $(".LeftColumn").fadeOut("fast");
            return false;
        });

        $(document).mouseup(function () {
            $('.LeftColumn').fadeOut('fast');
            $('.SidebarToggle').removeClass("active");
            $('.SidebarToggle').text("Show Sidebar");
        })
    });

The problem I have is when the user clicks elsewhere on the page it will hide the LeftColumn as I want, but the Toggle function doesn't know this so when the user clicks the SidebarToggle link it wont show the LeftColumn as it treats it as hiding. How can I fix this?

Thanks

+1  A: 
$(document).ready(function() {
    var leftColumn = $('.LeftColumn');
    leftColumn.hide();
    var sidebarToggle = $('.SidebarToggle')
    var hideSidebar = function() {
        sidebarToggle.removeClass('active');
        sidebarToggle.text('Show Sidebar');
        sidebarToggle.attr('title', 'Show Sidebar');
        leftColumn.fadeOut('fast');
    };
    var showSidebar = function() {
        sidebarToggle.addClass('active');
        sidebarToggle.text('Hide Sidebar');
        sidebarToggle.attr('title', 'Hide Sidebar');
        leftColumn.fadeIn('fast');
    };
    sidebarToggle.click(function() {
        if (sidebarToggle.hasClass('active')) {
            hideSidebar();
        }
        else {
            showSidebar();
        }
        return false;
    });
    $(document).click(function () {
        if (sidebarToggle.hasClass('active')) {
            hideSidebar();
        }
    });
});
Andreas Niedermair
The button no longer closes the LeftColumn though, you can only close it by clicking elsewhere on the page. I want to close it for both actions.
Cameron
+1  A: 

In this case, instead of .toggle(), use .click() and detect the state inside by checking for that active class with .hasClass(), like this:

$(function () {
  $(".LeftColumn").hide().click(function(e) { e.stopPropagation(); }); 
  $(".SidebarToggle").click(function () {
    if($(this).hasClass("active")) {
      $(this).removeClass("active")
             .text("Show Sidebar")
             .attr("title", "Show Sidebar");
      $(".LeftColumn").fadeOut("fast");      
    } else {
      $(this).addClass("active")
             .text("Hide Sidebar")
             .attr("title", "Hide Sidebar");
      $(".LeftColumn").fadeIn("fast");
    }
    return false;
  });
  $(document).click(function () {
      $('.LeftColumn').fadeOut('fast');
      $('.SidebarToggle').removeClass("active")
                         .text("Show Sidebar");
  });
});

You can try a demo here

This way, you don't need to worry about the state of the .toggle() functions, you're checking as you click :)

Nick Craver
The button no longer closes the LeftColumn though, you can only close it by clicking elsewhere on the page. I want to close it for both actions.
Cameron
@Cameron - Does the button start out with the `active` class?
Nick Craver
------ @Nick No
Cameron
@Cameron - Try now...didn't notice you were using `mouseup` on `document`...since you were using `return false;` on the `click` event, it wasn't bubbling to `document` to trigger its handler as well...but it was listening for `mouseup` which *does* still bubble...resulting in a hide 100% of the time. To prevent a click on the left bar from also closing it (it'll bubble to `document` too), add a `$(".LeftColumn").click(e) { e.stopPropagation(); });` in there :)
Nick Craver
@Nick where do I add that line? $(".LeftColumn").click(e) { e.stopPropagation(); });
Cameron
@Cameron - Anywhere in your document.ready, for example at the top, chain it with the `.hide()`, like this: `$(".LeftColumn").hide().click(e) { e.stopPropagation(); });`
Nick Craver
That breaks it, apparently because of the stray ) on the end of that line. Could you edit your answer with this added?
Cameron
@Cameron - Oh wow, I completely left part of that out, like this: `$(".LeftColumn").hide().click(function(e) { e.stopPropagation(); });`, I'll edit to include.
Nick Craver
Yeah that nailed it. Thanks.
Cameron
A: 

give this a shot

$(document).mouseup(function (e) {
    if (! $(e.target).is('.SidebarToggle')) {
        $('.LeftColumn').fadeOut('fast');
        $('.SidebarToggle').removeClass("active");
        $('.SidebarToggle').text("Show Sidebar");
    }
})
Reigel
This issue isn't the hiding...that works, the problem is it was hidden, but not via the `.toggle()`, meaning it didn't swap which function it was using, so the next `click` event *also* tries to hide it...because that's the function next in the `.toggle()` rotation :)
Nick Craver