Implementing a Collapsible Accordion in a Jira Plugin with Velocity Template

I am implementing a collapsible info panel in a Jira plugin using a Velocity template. I need an accordion solution, as my current implementation fails. See sample code below:

<button class="toggle-btn">Item A</button>
<div class="toggle-content">
  <p>Example text for Item A.</p>
</div>

<script>
  const items = document.querySelectorAll('.toggle-btn');
  items.forEach(item => {
    item.addEventListener('click', () => {
      const panel = item.nextElementSibling;
      panel.style.display = (panel.style.display === 'block') ? 'none' : 'block';
    });
  });
</script>

In my experience, the issue typically arises from when the script is executed rather than the logic of the accordion itself. I solved something similar by ensuring the DOM was fully loaded before running the script, using a document ready event wrapper. Additionally, I relied on a more robust event delegation approach, especially when multiple dynamic elements are rendered asynchronously. Confirming that there were no CSS conflicts with display properties for nested elements was crucial. Testing scrolling conflicts and verifying the script order in your Velocity template often resolves these unforeseen issues.

I too encountered several hurdles working on a similar collapsible panel in a Jira plugin. In my experience, sticking with the core logic but rethinking the script placement made a significant difference. For instance, I had to ensure that all DOM elements were fully rendered before binding event listeners. Additionally, validating that the Velocity template processed the entire markup correctly was crucial. A small experiment I made was adding logging statements to trace element availability, which helped pinpoint timing issues that were otherwise hard to detect.