MultiStep Form jQuery Plugin
plug n play
by Cone
HTML
<form id="myForm" action="/register.php">
<h1>Register:</h1>
<!-- One "tab" for each step in the form: -->
<div class="tab">Name:
<p><input placeholder="First name..." name="fname"></p>
<p><input placeholder="Last name..." name="lname"></p>
</div>
<div class="tab">Contact Info:
<p><input placeholder="E-mail..." name="email"></p>
<p><input placeholder="Phone..." name="phone"></p>
</div>
<div class="tab">Birthday:
<p><input placeholder="dd" name="dd"></p>
<p><input placeholder="mm" name="nn"></p>
<p><input placeholder="yyyy" name="yyyy"></p>
</div>
<div class="tab">Login Info:
<p><input placeholder="Username..." name="uname"></p>
<p><input placeholder="Password..." name="pword" type="password"></p>
</div>
<div style="overflow:auto;">
<div style="float:right;">
<button type="button" class="previous">Previous</button>
<button type="button" class="next">Next</button>
<button type="submit">Save</button>
</div>
</div>
<!-- Circles which indicates the steps of the form: -->
<div style="text-align:center;margin-top:40px;">
<span class="step">1</span>
<span class="step">2</span>
<span class="step">3</span>
<span class="step">4</span>
</div>
</form>
CSS
* {
box-sizing: border-box;
}
.tab {
display: none;
width: 100%;
height: 50%;
margin: 0px auto;
}
.current {
display: block;
}
body {
background-color: #f1f1f1;
}
form {
background-color: #ffffff;
margin: 50px auto;
font-family: Raleway;
padding: 40px;
width: 70%;
min-width: 300px;
}
h1 {
text-align: center;
}
input {
padding: 10px;
width: 100%;
font-size: 17px;
font-family: Raleway;
border: 1px solid #aaaaaa;
}
button {
background-color: #4CAF50;
color: #ffffff;
border: none;
padding: 10px 20px;
font-size: 17px;
font-family: Raleway;
cursor: pointer;
}
button:hover {
opacity: 0.8;
}
.previous {
background-color: #bbbbbb;
}
/* Make circles that indicate the steps of the form: */
.step {
height: 30px;
width: 30px;
cursor: pointer;
margin: 0 2px;
color: #fff;
background-color: #bbbbbb;
border: none;
border-radius: 50%;
display: inline-block;
opacity: 0.8;
padding: 5px
}
.step.active {
opacity: 1;
background-color: #69c769;
}
.step.finish {
background-color: #4CAF50;
}
JavaScript
$(document).ready(function() {
$("#myForm").multiStepForm({
// defaultStep:0,
beforeSubmit: function() {
console.log("save");
}
}).navigateTo(0);
});
/* PLUGIN CODE STARTS FROM HERE*/
(function($) {
$.fn.multiStepForm = function(args) {
if (args === null || typeof args !== 'object' || $.isArray(args))
throw " : Called with Invalid argument";
var form = this;
var tabs = form.find('.tab');
var steps = form.find('.step');
steps.each(function(i, e) {
$(e).on('click', function(ev) {
form.navigateTo(i);
});
});
form.navigateTo = function(i) {
/*index*/
/*Mark the current section with the class 'current'*/
tabs.removeClass('current').eq(i).addClass('current');
// Show only the navigation buttons that make sense for the current section:
form.find('.previous').toggle(i > 0);
atTheEnd = i >= tabs.length - 1;
form.find('.next').toggle(!atTheEnd);
// console.log('atTheEnd='+atTheEnd);
form.find('.submit').toggle(atTheEnd);
form.fixStepIndicator(form.curIndex());
return form;
}
form.curIndex = function() {
/*Return the current index by looking at which section has the class 'current'*/
return tabs.index(tabs.filter('.current'));
}
form.fixStepIndicator = function(n) {
steps.each(function(i, e) {
i == n ? $(e).addClass('active') : $(e).removeClass('active');
});
}
/* Previous button is easy, just go back */
form.find('.previous').click(function() {
form.navigateTo(form.curIndex() - 1);
});
/* Next button goes forward iff current block validates */
form.find('.next').click(function() {
if ('validations' in args && typeof args.validations === 'object' && !$.isArray(args.validations) && (!('noValidate' in args) || (typeof args.noValidate === 'boolean' && !args.noValidate))) {
form.validate(args.validations);
if (form.valid() == true) {
...