Program:
<html>
...
<button class="accordion">Section 1</button>
<div class="panel"> <p> .... </p> </div>
<button class="accordion">Section 2</button>
<div class="panel"> <p> .... </p> </div>
<button class="accordion">Section 3</button>
<div class="panel"> <p> .... </p> </div>
<script>
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function(){
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
</script>
...
</html>
I am a newbie to javascript. So, I am not able to understand the above javascript code. I expect "this" keyword refers to window object. Because, it is called within anonymous function and also there is no object for that function. But, it refers to the object "HTMLButtonElement". How "this" refers to Button object ?