Recently I was asked if it would be possible to have the managed metadata navigation TreeView in SharePoint 2013 automatically expand when the page was first loaded.
While I have seen some articles in the past for SharePoint 2010 to modify the MetadataNavTree user control file in the Templates directory, this solution is not possible while using Office 365 or SharePoint 2013 Online.
I figured there would be a way to use some client-side scripting with jQuery to toggle the navigation node so I started looking through the DOM and events on the managed metadata navigation nodes.
I started by getting the id of the
container for the term set in the treeview. It should be similar to "ctl00_PlaceHolderLeftNavBar_ctl02_WebTreeViewn1
which does a post-back call to the server to dynamically load the terms and render the navigation. This is done so the initial page load performance doesn't have an impact if you have a large term set.
After clicking on the term set to load the terms (nodes), I noticed the DOM changed for the term set node and a new method was added to the
container called "TreeView_ToggleNode
So after a user expands the term set at least once, the data is loaded and it just calls another method to collapse/expand the nodes.
Auto-expand Managed Metadata Navigation code: