Skip to content

Instantly share code, notes, and snippets.

@aackose
Last active March 25, 2018 22:12
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 aackose/42e0e0c47e4625ec5c30f7d8803aff9a to your computer and use it in GitHub Desktop.
Save aackose/42e0e0c47e4625ec5c30f7d8803aff9a to your computer and use it in GitHub Desktop.
Converts a WFFM Form Sections to a Wizard Form
// Insert an HTML based "Previous" and "Next" button right next to the "Submit" button
// Hide the "Previous" button on the initial load
// Add "submit" class to the existing "Submit" button for ease of targeting
$("<input type='submit' value='Previous' class='prev' onclick='return false;' style='display:none'/><input type='submit' value='Next' class='next' onclick='return false;'/>").insertBefore($(".scfForm input[type='submit']").addClass('submit').hide());
// Capture all the <fieldsets> a.k.a Sections in the WFFM Form
// Note that the Form has a class "scfForm", which is the OOTB class name
// If you have multiple WFFM Forms on the page, you will have to scope this to the current Form
var fieldSets = $(".scfForm fieldset");
var current = 0;
// Hide all the WFFM sections except the first one
fieldSets.hide().first().show();
// On click on Next button:
// - Hide the current section and show the next section
// - Show/Hide the Previous, Next and Submit buttons accordingly
$(".scfForm .next").click(function() {
fieldSets.eq(current).hide();
current = (current + 1 < fieldSets.length) ? current + 1 : 0;
fieldSets.eq(current).show();
evalActions(current,fieldSets.length-1);
});
// On click on Previous button:
// - Hide the current section and show the previous section
// - Show/Hide the Previous, Next and Submit buttons accordingly
$(".scfForm .prev").click(function() {
fieldSets.eq(current).hide();
current = (current > 0) ? current - 1 :fieldSets.length - 1;
fieldSets.eq(current).show();
evalActions(current,fieldSets.length-1);
});
// Generic method to evaluate Previous/Next/Submit button visibility based on the section of the form
function evalActions(i,l) {
// In the initial section, show "Next" button while hide "Previous" and "Submit" button
i == 0 && $(".scfForm .next").show() && $(".scfForm .prev,.scfForm .submit").hide();
// In the final section, show "Submit" and "Previous" buttons while hide "Next" button
i == l && $(".scfForm .prev,.scfForm .submit").show() && $(".scfForm .next").hide();
// In the intermediate sections, show "Previous" and "Next" buttons while hide "Submit" button
(i > 0 && i < l) && $(".scfForm .prev,.scfForm .next").show() && $(".scfForm .submit").hide();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment