Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Last active January 15, 2018 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subtleGradient/9947aa35d4bbf83cf24458297a648e41 to your computer and use it in GitHub Desktop.
Save subtleGradient/9947aa35d4bbf83cf24458297a648e41 to your computer and use it in GitHub Desktop.
Fix SharePoint left sidebar selecting the wrong link with JavaScript
function fixLeftNavSelection() {
const leftNavBar = document.getElementById("DeltaPlaceHolderLeftNavBar");
if (leftNavBar == null) return;
const href = location.pathname + location.search;
const correctLink = leftNavBar.querySelector(
'li > a.ms-core-listMenu-item[href*="' + href + '"]'
);
if (correctLink == null) return;
// Remove selection from everything
var selectednav = leftNavBar.querySelector(
"li.selected > a.ms-core-listMenu-selected"
);
if (selectednav != null) {
selectednav.classList.remove("ms-core-listMenu-selected");
selectednav.parentElement.classList.remove("selected");
}
// Add selection to the correct link and li
correctLink.classList.add("ms-core-listMenu-selected");
correctLink.parentElement.classList.add("selected");
}
if (typeof _spBodyOnLoadFunctionNames === "object") {
_spBodyOnLoadFunctionNames.push("fixLeftNavSelection");
}
@subtleGradient
Copy link
Author

Keep in mind that this is ES6 code. You may want to convert it to ES5 with Babel before deploying it live

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment