0

I created a simple accordion with plain JavaScript, no jQuery. It dynamically is created by the number of data given in an external JavaScript file in JSON format.

The accordion looks like that in the html code:

<div id="description"> 
<script language="javascript" type="text/javascript">
startAccordion();
function startAccordion() 
{
    for (var i=1; i<=Object.keys(description).length; i++){
        document.getElementById("description").innerHTML += 
            "<button class='accordion'>Trail " + description[i].Title + "</button>" + "<div class='accordion-panel'>" + "<p>" + description[i].Description + "<br></p>" + "</div>";
    }
}
</script>

Within the JavaScript the behavior when a panel on the accordion is clicked, is handled.

var acc = document.getElementsByClassName('accordion');
var panel = document.getElementsByClassName('accordion-panel');

for (var i = 0; i < acc.length; i++) 
{
  acc[i].onclick = function()
  {         
    // Toggle between adding and removing the "active" class,
    //to highlight the button that controls the panel 
    this.classList.toggle("active");

    //Toggle between hiding and showing the active panel
    var panel = this.nextElementSibling;

    if (panel.style.display === "block") {
        panel.style.display = "none";
    } else {
        panel.style.display = "block";
    }
  }
}

Question: When the user clicks on a panel and this certain one is opened/activated, is there any possibility to get the index or any unique information about which of the panels has been clicked?

I really need this information, because based on that, the related marker on a map should be shown. I tried for hours and hours, but I could not find out, how to get the panel index. The variable "i" is unfortunately always 20, as 20 features are included. I think this is strange, because with acc[i] the related panel is opened/activated correctly.

I would like to avoid using jQuery, but if you know that with my solution it is impossible I maybe could use jQuery, although I would have to create the whole accordion again.

the_chimp
  • 205
  • 4
  • 18
  • 2
    Check this https://stackoverflow.com/questions/8801787/get-index-of-clicked-element-using-pure-javascript – Mohamed Abbas Jun 27 '17 at 19:34
  • Anything in jQuery is possible in Javascript, after all jQuery is written in JS, and in a lot of modern browsers there are a lot of useful new features that resemble jQuery's. – David Archibald Jun 27 '17 at 19:35
  • thanks so much @Mohamed Abbas - the solution posted on the linked question solved my problem! – the_chimp Jun 27 '17 at 19:53

1 Answers1

0

Following the link provided by Mohamed Abbas, this is the way it worked out. It is a scope issue, where within the for loop a new scope is created:

var acc = document.getElementsByClassName('accordion');
for (var i = 0, i < acc.length; i++)
{

(function(index){
    acc[i].onclick = function(){

    //same as within the function given in the original post

          alert(index)  ;
    }    
})(i);

}
the_chimp
  • 205
  • 4
  • 18