Step Wizard
Build a reusable step wizard plugin using jQuery plugin pattern with a simple public API.
Build a reusable step wizard plugin using jQuery plugin pattern with a simple public API.
$.fn.wizard = function(opts = {}) {
return this.each(function() {
const $w = $(this);
const $steps = $w.find(".wiz-step");
let cur = opts.start || 0;
function render() {
$steps.hide().eq(cur).show();
$w.find(".wiz-progress").css("width", ((cur+1)/$steps.length*100)+"%");
$w.trigger("wizard:step", [cur, $steps.length]);
}
$w.on("click",".wiz-next",() => { if (cur < $steps.length-1) { cur++; render(); } });
$w.on("click",".wiz-prev",() => { if (cur > 0) { cur--; render(); } });
$w.data("wizard", { goTo: n => { cur=n; render(); } });
render();
});
};
$("#onboarding").wizard({ start: 0 });
Trigger custom events (wizard:step) so parent pages can react to step changes without modifying the plugin.