Solution 1 :

In the example you submit section there is a function called OpenCity that passes a event. To stop it from refreshing or submitting you just have to prevent the default state:

<script>
function openCity(evt, cityName) {

   evt.preventDefault();

   ...
}
</script>
function openCity(evt, cityName) {
	
    evt.preventDefault();

  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";
}
body {font-family: Arial;}

/* Style the tab */
.tab {
  overflow: hidden;
  border: 1px solid #ccc;
  background-color: #f1f1f1;
}

/* Style the buttons inside the tab */
.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  transition: 0.3s;
  font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
  background-color: #ddd;
}

/* Create an active/current tablink class */
.tab button.active {
  background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
  display: none;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}

.button {
  padding:15px;
  border: none 0px white;
  border-color: white;
  background-color: #318DE8;
  border-radius: 3px;
  color: white;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>

<h2>Tabs</h2>
<p>Click on the buttons inside the tabbed menu:</p>

<div class="tab">
  <button class="tablinks" onclick="openCity(event, 'London')">London</button>
  <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
  <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>

<form>
<div id="London" class="tabcontent">
  <h3>London</h3>
  <p>London is the capital city of England.</p>
  <input name="value_1" value="" placeholder="Insert Value that will remain when switched"/>
</div>

<div id="Paris" class="tabcontent">
  <h3>Paris</h3>
  <p>Paris is the capital of France.</p>
  <input name="value_2" value="" placeholder="Insert Value that will remain when switched"/>
</div>

<div id="Tokyo" class="tabcontent">
  <h3>Tokyo</h3>
  <p>Tokyo is the capital of Japan.</p>
  <input name="value_3" value="" placeholder="Insert Value that will remain when switched"/>
</div>
<br>
<input class="button" type="submit" value="Submit Form" />
</form>


   
</body>
</html> 

Problem :

Referring to this example on how to create a tabbed form in html: the example works as expected until you embed the code within a <form> element. Then clicking the tabs causes a form submission, and in the case of the example, an error.

Does anyone know a way out of this?

Comments

Comment posted by Andrew Halpern

Can you share some code for what you tried?

Comment posted by Agnar Renolen

Click the link above, and then click “Try it Yourself”. Try clicking the tabs on the right. Then edit the code so the entire “form” is inside a

and try again.

Comment posted by Agnar Renolen

Click on the buttons inside the tabbed menu:

By