Collapsible accordion spreads and collapse instantly

I have a collapsible accordion placed inside a form. Pressing the button to spread, the accordion spreads and collapse back instantly. If I don’t put it in the form, it works fine for me. This is the simplified code: index.html:

    <meta charset="UTF-8">
    <link rel="stylesheet" href="estilos.css">
    <form  method="post" id="platos">
    <button class="accordion">Section 1</button>
    <div class="panel">
    <p>Lorem ipsum...</p>

    <button class="accordion">Section 2</button>
    <div class="panel">
    <p>Lorem ipsum...</p>

    <button class="accordion">Section 3</button>
    <div class="panel">
    <p>Lorem ipsum...</p>
    <script src="collapsible.js"></script>


var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    var panel = this.nextElementSibling;
    if ( === "block") { = "none";
    } else { = "block";


For each button inside the form tag, specify type="button", and these buttons will stop working as a submit:

<button type="button" class="accordion">Section</button>