I had code in my Rails application that successfully closed a toggled sidebar when clicking on X. I made other changes that somehow stopped my code from working. When I hover over the x it highlights as the other links but when I click it the window remains open and /#
is appended at the end of the current link. When I click the other links in the sidebar they display the correct view and the sidebar closes. I changed something that I cannot find.
My sidebar where the X is defined:
<ul class="sidebar-nav">
<li class="sidebar-close"><%= link_to "#" do %><i class="fa fa-times"></i><% end %></li>
.....
</ul>
CSS for sidebar-close:
.sidebar-nav > .sidebar-close { height: 50px; font-size: 150%; line-height: 200%; color: $sideBarClose; text-align: right; padding-right: 10px; }
.sidebar-nav > .sidebar-close a { color: $sideBarLink; }
.sidebar-nav > .sidebar-close a:hover { color: $white; background: none; }
My menu button:
<div class="menu-button"><%= link_to "#menu-toggle", class: "btn btn-default btn-lg", id: "menu-toggle" do %><i class="fa fa-bars"></i><%= " #{t :menu}" %><% end %></div>
CSS for the menu button:
.menu-button { padding-top: 10px; position: absolute; left: 15px; top: 0px; z-index: 1; }
Script (includes code to close the window if you click outside of the sidebar:
$("body, html").not('#menu-toggle').click(function(e) {
console.log(e.target);
if($('#wrapper').hasClass("toggled") && !$(e.target).is('#menu-toggle, #sidebar-wrapper, #sidebar-wrapper *'))
{
//alert('asd');
$("#menu-toggle").trigger("click");
}
});
$("#menu-toggle").click(function(e) {
e.preventDefault();
$("#wrapper").toggleClass("toggled");
});
I will mention that I did not have any special Javascript for this to work. I'm just not seeing what I changed where clicking X no longer closes the window.
Update: With help from @Justastudent I was able to create a JSFiddle. When you click the Menu button it toggles just fine. If you have the sidebar open and click outside of the sidebar the sidebar will close. If you click the X on the top right of the sidebar it does not work. It seems to get stuck. When the X is clicked on my system the page scrolls down and I cannot scroll it back to the top. I have to close the tab and access the link in new tab to get the top of the fiddle page to display.