Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 17, 2023 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/27a3762ececcedcc8f8c039b08fa823a to your computer and use it in GitHub Desktop.
Save code-boxx/27a3762ececcedcc8f8c039b08fa823a to your computer and use it in GitHub Desktop.
Simple HTML Javascript Wizard Form

JAVASCRIPT HTML WIZARD FORM

https://code-boxx.com/simple-html-js-wizard-form/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

/* (A) ENTIRE PAGE */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
/* (B) WIZARD FORM */
#wiz {
max-width: 600px;
padding: 15px;
border: 1px solid #eee;
background: #f7f7f7;
}
#wiz .step { display: none; }
#wiz .now { display: block !important; }
/* (C) WIZARD FORM FIELDS */
#wiz label, #wiz input[type=text], #wiz input[type=email] {
display: block;
width: 100%;
}
#wiz label { margin: 10px 0; }
#wiz input[type=text], #wiz input[type=email] {
border: 1px solid #f2f2f2;
padding: 10px;
}
#wiz input[type=button], #wiz input[type=submit] {
padding: 10px;
margin-top: 20px;
border: 0;
color: #fff;
background: #2d32d5;
cursor: pointer;
}
<!DOCTYPE html>
<html>
<head>
<title>JS Wizard Form Demo</title>
<meta charset="utf-8">
<!-- (A) LOAD CSS + JS -->
<link rel="stylesheet" href="wizard.css">
<script src="wizard.js"></script>
</head>
<body>
<!-- (B) WIZARD FORM -->
<div id="wiz">
<form class="step">
<h3>STEP 1 - CHOOSE A MEME ANIMAL</h1>
<label>
<input type="radio" name="meme" value="Doge" required> Doge
</label>
<label>
<input type="radio" name="meme" value="Cate" required> Cate
</label>
<input type="submit" value="Next">
</form>
<form class="step">
<h3>STEP 2 - YOUR NAME & EMAIL</h1>
<label>Name</label>
<input type="text" name="name" required>
<label>Email</label>
<input type="email" name="email" required>
<input type="button" class="back" value="Back">
<input type="submit" value="Go!">
</form>
</div>
</body>
</html>
function wiz () {
// (A) FLAGS & PROPERITES
let hSteps = document.querySelectorAll("#wiz form.step"), // wizard steps
steps = hSteps.length - 1, // total number of steps
now = 0; // current step
// (B) LAST STEP / NEXT STEP / SUBMIT
let proceed = next => {
// (B1) UPDATE CURRENT STEP
if (next) { now++; } else { now--; }
// (B2) THIS IS THE LAST STEP
// @TODO - COMPLETE THIS ON YOUR OWN!
if (now>steps) {
/* (B2-1) AJAX SUBMIT?
let form = new FormData();
for (let s of hSteps) {
for (let [k,v] of new FormData(s)) { form.append(k,v); }
}
fetch("SERVER-SCRIPT", { method:"post", body:form })
.then(res => res.text())
.then(txt => alert(txt));
*/
/* (B2-2) REDIRECT WITH URL SEARCH PARAMS?
let params = new URLSearchParams();
for (let s of hSteps) {
for (let [k,v] of new FormData(s)) { params.append(k,v); }
}
location.href = "URL?" + params.toString();
*/
}
// (B3) SHOW LAST/NEXT STEP
else { for (let i=0; i<=steps; i++) {
if (i==now) { hSteps[i].classList.add("now"); }
else { hSteps[i].classList.remove("now"); }
}}
// (B4) PREVENT DEFAULT HTML FORM SUBMIT
return false;
};
// (C) AUTO ATTACH LAST STEP / NEXT STEP / SUBMIT
for (let i=0; i<=steps; i++) {
// (C1) LAST STEP - IF ANY
let back = hSteps[i].querySelector(".back");
if (back) { back.onclick = () => proceed(); }
// (C2) NEXT STEP
hSteps[i].onsubmit = () => proceed(true);
}
// (D) READY - SHOW FIRST STEP
hSteps[0].classList.add("now");
}
window.addEventListener("DOMContentLoaded", wiz);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment