Search code examples
javascriptjqueryajaxwordpressgravity-forms-plugin

gravity forms Displaying a message from dropdown select


Is it possible to display message/description when a user chooses an option from a dropdown menu.

Example:

if user chooses "London" from the dropdown menu then we want to display a message from the a database/file relating to "London" ideally using Ajax so not to reload the page.

Thanks in advance.


Solution

  • Yes it is possible. You can add a CSS Class Name to the select in Gravity Forms. This will make it easy for you to add an event listener via your custom Javascript/jQuery. You will need your ajax endpoint of course, but you can add a listener like so:

    (function ($) {
        $(function () {
            // Set your variables here
            var select_class = 'my-select-class',
                endpoint = '/my_endpoint.php',
                notification_class = 'notification-for-select';
    
            var $my_select = $('.' + select_class);
            $my_select.parent().prepend('<div class="' + notification_class + '"></div>');
            $my_select.change(function (e) {
                var my_data = $my_select.val();
                $.post(endpoint, my_data, function (data) {
                    $('.' + notification_class).text(data);
                });
            });
        });
    })(jQuery);