Jump to content

Active links in Archive Block

Go to solution Solved by jpeter,

Recommended Posts

Hello everyone! I'm encountering an issue with customizing the filter (archive block). Specifically, I'm looking to give active links a distinct style, such as underlining or making them bold when selected. For instance, if I choose "documentaries" and it filters content accordingly, I'd like the link to be visibly active. Despite trying various codes, I haven't found a solution. A friend knowledgeable in coding, although not specifically with Squarespace, suggested that using JavaScript might be the way to go.

Has anyone else faced a similar challenge? I'd greatly appreciate any advice or suggestions. Thank you!

Link to the page: https://www.sandranam.me/work

Pass: 7u5PY6B6C

Link to comment
  • Solution

@maya_m

STEP 1: 
Add the following JavaScript code to the Footer using Code Injection.  This script adds an "active" class on the link that matches whatever filter is defined in the URL. 

(function () {
  // Get all the links inside the specified container
  const links = document.querySelectorAll("#block-yui_3_17_2_1_1706047640137_2381 a");

  // Loop through each link and check if its path matches the path in the URL
  links.forEach((link) => {
      // Add the "active" class if the pathname of in the URL matches 
      if (window.location.pathname === link.pathname) {
          link.classList.add("active");
      }
  });
})();

Be sure to add the JavaScript code above between <script> tags like so:

<script>
  // Add JS here
</script>

 

Step 2:
You can now style the class like so using the following CSS:

/* Style active filter link */
#block-yui_3_17_2_1_1706047640137_2381 .active {
    color: blue;
}

 

Edited by jpeter

Full stack developer who loves helping people out with anything web related. If you'd like to support me, buy me a coffee!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.