Hey @Support-Luis sorry I should have clarified, the tab links are in the nav and so the user would be coming from another page. So at page load, the anchor is not on the page because the CMS tabs hasn’t loaded the anchor section yet. Does that make sense? Any solution to that?
Instead of trying to scroll the page with the anchor links, we set an id to the div containing the CMS Tabs. We then use JS to detect which option the user selected on the nav bar and make the required tab active when we scroll the page to this section.
We can add the current id as attributes to both the links on the nav and the tab link elements on the CMS Tabs. Something like data-tab = ID should be fine. If you can make these changes I can work the code!
Hope this makes sense. If needed, I can record a quick loom or hop on a call!
I’ve got this code to work, however, there are some changes you’ll need to make for it to work on your end!
Add this code to the </body> section of custom code in the site settings, as this code is needed on all pages.
<script>
document.addEventListener('DOMContentLoaded', function () {
const tabAnchors = document.querySelectorAll('[data-tab]');
tabAnchors.forEach((tab) => {
tab.addEventListener('click', () => {
const anchor = tab.getAttribute('data-tab');
localStorage.setItem('tabToOpen', anchor);
});
});
const tabToOpen = localStorage.getItem('tabToOpen');
if (tabToOpen) {
const target = document.getElementById(tabToOpen);
if (target) {
setTimeout(() => {
target.click();
}, 100);
} else {
console.warn(`No tab found with ID "${tabToOpen}"`);
}
localStorage.removeItem('tabToOpen');
}
});
</script>
I removed the ID previously used as anchor links from the collection items and added a new one to the element below. It can be anything, I am just using “tabs” to demonstrate this.