1

I implemented below piece of code for accordion from w3school here and wrapped it in a form tag. When I tried to run it, I clicked the accordion header, the page keeps on refreshing. How to prevent this? Below is my code based on w3school.

<form name="formone" method="post" action="file.php">
<button onclick="myFunction('Demo1')" class="w3-btn w3-block w3-black w3-left-align">Vehicle</button>
<div id="Demo1" class="w3-container w3-hide">
  <input type="checkbox" name="vehicle" value="Bike"> Bike<br>
  <input type="checkbox" name="car" value="Car"> Car<br>
</div>
<button onclick="myFunction('Demo2')" class="w3-btn w3-block w3-black w3-left-align">Distance</button>
<div id="Demo2" class="w3-container w3-hide">
  <input type="checkbox" name="distance" value="m"> Metre<br>
  <input type="checkbox" name="distance" value="km"> KM<br>
</div>
<br>
<button type="submit" name="submit">Submit</button>
</form>

</div>

<script>
function myFunction(id) {
    var x = document.getElementById(id);
    if (x.className.indexOf("w3-show") == -1) {
        x.className += " w3-show";
    } else { 
        x.className = x.className.replace(" w3-show", "");
    }
}
</script>

Here is my JSFiddle

Athirah Hazira
  • 473
  • 2
  • 15
  • 37

1 Answers1

4

You have to give type tag in button element to specify it is not a submit button

<button type="button">

https://jsfiddle.net/e3raaywm/1/