Home / Admin / How to Conditionally Show the Next Button on a Specific Page
Duplicate Snippet

Embed Snippet on Your Site

How to Conditionally Show the Next Button on a Specific Page

<10
Code Preview
php
<?php
/**
 * Hide Next button on page 2 by default and show it once an option in a field (e.g. Multiple Choice) is selected.
 */
// Add CSS to hide the Next button on page 2.
add_action( 'wp_head', function () { ?>
    <style>  
        /* Hide Next button on page 2 */
        #wpforms-form-1000 button.wpforms-page-button.wpforms-page-next[data-page="2"] {
            display: none;  
        }
        /* Class to show Next button once an option is selected */  
        #wpforms-form-1000 button.wpforms-page-button.wpforms-page-next[data-page="2"].show-next {
            display: inline-block;
        }
    </style>
<?php } );
// Add JS to show the Next button after an option is selected in the field.
add_action( 'wpforms_wp_footer_end', function() { ?> 
    <script>
    jQuery(function($){
        var formSelector = "#wpforms-form-1000"; 
        // Listen for changes in the field with ID 10.
        $(formSelector).on('change', "input[name='wpforms[fields][10]']", function(){ 
            var selectedval = $(formSelector + " input[name='wpforms[fields][10]']:checked").val();
            if (selectedval) {
                // If an option is selected, add the show-next class to the Next button on page 2.
                $(formSelector + " .wpforms-page-button.wpforms-page-next[data-page='2']").addClass("show-next"); 
            }
        });
    });
    </script>
<?php } );

Comments

Add a Comment