Skip to content Skip to sidebar Skip to footer

Simple Javascript Accordion - How To Get The Index Of The Clicked Panel?

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 lo

Solution 1:

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);

}

Post a Comment for "Simple Javascript Accordion - How To Get The Index Of The Clicked Panel?"