How can we help you today?

How to Create an Anchor Link that Opens to a Specific Accordion Item

This article will show you how to create an anchor link that redirects to a specific accordion item:


sample anchor links


TABLE OF CONTENTS


Prerequisites


sample accordions


  • You must already have a Design Element that allows linking of multiple items, including Tiles, Simple List, Icon List, etc.


sample Tiles


Step-by-step Tutorial

Follow the steps below to learn how to create the anchor link:


Step 1: Edit the ShortPoint Web Part

Go to the SharePoint page you want to use and click Edit:


Edit


Close the Toolbox:


close icon


Click the Edit Properties icon:


Edit properties icon


Step 2: Edit the Accordions Design Element


NOTEBefore proceeding, make sure you already have an Accordions Design Element on your page. If you don’t have one yet, check out How to Customize the Accordions Design Element. 

sample accordions


Click the Accordions tag:


Accordions tag


Select the cogwheel icon:


cogwheel icon


Step 3: Add a Class Name to Every Accordion Item

Click the cogwheel icon of the first item:


Cogwheel icon


Go to the Custom CSS tab:


Custom CSS tab


In the Class Name field, write 'sp-tiles-click-number'. Replace ‘number’ with the actual position of your item in the sequence. For example, since this is the first item, use '1'; for the second item, use '2', and so on.


add class name


NOTEMake sure to take note of this class name as you will need it in the next steps.


Click the back icon:


back icon


Repeat step 3 for every item you have inside the Accordions:


repeat steps


Once done, click the green check mark:


green check mark


Step 4: Edit a Design Element


NOTEYou must already have a Design Element that allows linking of multiple items, including Tiles, Simple List, Icon List, etc. 

sample Tiles


Select the EasyPass tag of the Design Element you want to use. For this guide, we’ll use Tiles:


Easypass tag


Click the cogwheel icon:


Cogwheel icon


Click the cogwheel icon of the first item:


Cogwheel icon


In the Link field, enter '#sp-tiles-click-number'. The number in this link must match the number you used in the Class Name from Step 3. For example, if your Class Name for this specific item is 'sp-tiles-click-1', then your Link should be '#sp-tiles-click-1'.


class


Click the back icon:


back icon


Repeat step 5 for the other items:


repeat steps


Step 6: Add a Class Name

Click the cogwheel icon of the first item:


cogwheel icon


Go to the Custom CSS tab:


Custom CSS tab


Under Class, write ‘sp-tiles-click’:


class


Click the back icon:


back icon


Repeat step 6 for the other items:


repeat steps


Once done, click the green check mark:


green check mark


Step 7: Copy JavaScript Code

Copy the JavaScript code below:

function openAccordion(accordionClassName) {
    var $ = shortpoint.base.libs.$;
    var $openItemOpenIcon = $(accordionClassName + ' .shortpoint-toggle-icon-open');
   
    // Simulate click on the open icon
    $openItemOpenIcon.trigger('click');
};

// Function to attach click listeners
function attachTileClickListeners() {
    const tiles = document.querySelectorAll(".sp-tiles-click");

    tiles.forEach(tile => {
        tile.addEventListener("click", handleTileClick);
    });
}

// Function to handle the click event
function handleTileClick(event) {
    let hrefValue = getNestedHref(event.currentTarget);
    if (typeof (hrefValue) == 'string' && hrefValue.includes('#')) {
        let className = hrefValue.substring(1); // Remove the leading '#'
        if (className) {
            scrollIntoView(className);
            openAccordion('.' + className);
        }
    }
}

function getNestedHref(tileDiv) {
    const anchor = tileDiv.querySelector("a");
    return anchor ? anchor.getAttribute("href") : null;
}

function scrollIntoView(className) {
    let elem = document.getElementsByClassName(className)[0];
    if (elem != null) {
        elem.scrollIntoView({
            block: 'center',
            behavior: 'smooth'
        });
    }
}


setTimeout(attachTileClickListeners, 1000);

Step 8: Insert a Code Design Element

Click the plus icon to open the library of Design Elements:


plus icon


Look for Code and select:


Code


Step 9: Paste JavaScript Code

Paste the JavaScript code in the JavaScript field:


paste code


Then, go to the Design tab:


Design tab


[1] Disable Sandbox Mode and click the [2] green check mark:


disable Sandbox Mode


Step 10: Save and Publish

Save your changes:


Save


Publish your page:


Publish


Congratulations! You’ve successfully created an anchor link that opens to a specific accordions item.


Related articles:

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.

World's best intranet sites are designed using ShortPoint

Get started today! Learn more
See all 95 topics

Start a trial

Ignite your vision. Install ShortPoint directly on your site, or play in sandbox mode. No credit card required.

Get started today

World’s best intranet sites are designed using ShortPoint

Thousands of companies using ShortPoint everyday to design, brand and build award winning intranet sites.

Get started Learn more