Last active
March 25, 2018 22:12
-
-
Save aackose/42e0e0c47e4625ec5c30f7d8803aff9a to your computer and use it in GitHub Desktop.
Converts a WFFM Form Sections to a Wizard Form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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