0

I have to make a multi-step form for a website. I've used HTML, CSS and JS, but the JS seems to not work. The "Next" button is not working at all. On the website all the questions appear together, but I have to do something different. The "next" button has to let other question appear when pushed, but is not like that.

Can you help me? Maybe I included the js in the worng way?

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <h4 class="modal-title" id="myModalLabel">Richiedi qui il tuo preventivo</h4>
    </div>
    <div class="modal-body">
        <div class="top-content">

            <div class="inner-bg">
                <div class="container">
                    <div class="row">
                        <div class="col-sm-5">

                            <div class="description">

                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-5 form-box">

                            <form role="form" action="" method="post" class="registration-form">

                                <fieldset>
                                    <div class="form-top">
                                        <div class="form-top-left">
                                            <h3>Parte 1</h3>
                                            <p>Racontati chi sei:</p>
                                        </div>
                                        <div class="form-top-right">
                                            <i class="fa fa-user"></i>
                                        </div>
                                    </div>
                                    <div class="form-bottom">
                                        <div class="form-group">
                                            <label class="sr-only" for="form-first-name">Nome</label>
                                            <input type="text" name="form-first-name" placeholder="Nome" class="form-first-name form-control" id="form-first-name">
                                        </div>
                                        <div class="form-group">
                                            <label class="sr-only" for="form-last-name">Cognome</label>
                                            <input type="text" name="form-last-name" placeholder="Cognome" class="form-last-name form-control" id="form-last-name">
                                        </div>
                                        <div class="form-group">
                                            <label class="sr-only" for="form-about-yourself">Nome della tua attività</label>
                                            <textarea name="form-about-yourself" placeholder="Nome della tua attività" 
                                                        class="form-about-yourself form-control" id="form-about-yourself"></textarea>
                                        </div>

                                        <button type="button" class="btn btn-next">Avanti</button>
                                    </div>
                                </fieldset>

                                <fieldset>
                                    <div class="form-top">
                                        <div class="form-top-left">
                                            <h3>Parte 2</h3>
                                            <p>Le tue informazioni di contatto:</p>
                                        </div>
                                        <div class="form-top-right">
                                            <i class="fa fa-key"></i>
                                        </div>
                                    </div>
                                    <div class="form-bottom">
                                        <div class="form-group">
                                            <label class="sr-only" for="form-email">Email</label>
                                            <input type="text" name="form-email" placeholder="Email" class="form-email form-control" id="form-email">
                                        </div>
                                        <div class="form-group">
                                            <label class="sr-only" for="form-number">Numero di telefono</label>
                                            <input type="number" name="form-number" placeholder="Numero di telefono" class="form-password form-control" id="form-password">
                                        </div>
                                        <!--<div class="form-group">
                                            <label class="sr-only" for="form-repeat-password">Repeat password</label>
                                            <input type="password" name="form-repeat-password" placeholder="Repeat password..." 
                                                        class="form-repeat-password form-control" id="form-repeat-password">
                                        </div>-->
                                        <button type="button" class="btn btn-previous">Indietro</button>
                                        <button type="button" class="btn btn-next">Avanti</button>
                                    </div>
                                </fieldset>

                                <fieldset>
                                    <div class="form-top">
                                        <div class="form-top-left">
                                            <h3>Parte 3</h3>
                                            <p>Dobbiamo avere ancora qualche informazione prima di calcolare il tuo preventivo:</p>
                                        </div>

                                    </div>
                                    <div class="form-bottom">
                                        <div class="form-group">
                                            <label class="sr-only" for="form-number">Quante persone servi al giorno?</label>
                                            <input type="text" name="form-facebook" placeholder="Numero" class="form-facebook form-control" id="form-question">
                                        </div>
                                        <div class="form-group">
                                            <label class="sr-only" for="form-question">Compri bottiglie di plastica o di vetro?</label>
                                            <input type="text" name="form-twitter" placeholder="Bottiglie di plastica o di vetro?" class="form-question form-control" id="form-question">
                                        </div>


                                        <button type="button" class="btn btn-previous">Indietro</button>
                                        <button type="submit" class="btn">Calcola!</button>
                                    </div>
                                </fieldset>

                            </form>

                        </div>
                    </div>
                </div>
            </div>
        </div>    
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      <button type="button" class="btn btn-primary">Save changes</button>
    </div>
  </div>
</div>

+function($) {
'use strict';

var modals = $('.modal.multi-step');

modals.each(function(idx, modal) {
    var $modal = $(modal);
    var $bodies = $modal.find('div.modal-body');
    var total_num_steps = $bodies.length;
    var $progress = $modal.find('.m-progress');
    var $progress_bar = $modal.find('.m-progress-bar');
    var $progress_stats = $modal.find('.m-progress-stats');
    var $progress_current = $modal.find('.m-progress-current');
    var $progress_total = $modal.find('.m-progress-total');
    var $progress_complete  = $modal.find('.m-progress-complete');
    var reset_on_close = $modal.attr('reset-on-close') === 'true';

    function reset() {
        $modal.find('.step').hide();
        $modal.find('[data-step]').hide();
    }

    function completeSteps() {
        $progress_stats.hide();
        $progress_complete.show();
        $modal.find('.progress-text').animate({
            top: '-2em'
        });
        $modal.find('.complete-indicator').animate({
            top: '-2em'
        });
        $progress_bar.addClass('completed');
    }

    function getPercentComplete(current_step, total_steps) {
        return Math.min(current_step / total_steps * 100, 100) + '%';
    }

    function updateProgress(current, total) {
        $progress_bar.animate({
            width: getPercentComplete(current, total)
        });
        if (current - 1 >= total_num_steps) {
            completeSteps();
        } else {
            $progress_current.text(current);
        }

        $progress.find('[data-progress]').each(function() {
            var dp = $(this);
            if (dp.data().progress <= current - 1) {
                dp.addClass('completed');
            } else {
                dp.removeClass('completed');
            }
        });
    }

    function goToStep(step) {
        reset();
        var to_show = $modal.find('.step-' + step);
        if (to_show.length === 0) {
            // at the last step, nothing else to show
            return;
        }
        to_show.show();
        var current = parseInt(step, 10);
        updateProgress(current, total_num_steps);
        findFirstFocusableInput(to_show).focus();
    }

    function findFirstFocusableInput(parent) {
        var candidates = [parent.find('input'), parent.find('select'),
                          parent.find('textarea'),parent.find('button')],
            winner = parent;
        $.each(candidates, function() {
            if (this.length > 0) {
                winner = this[0];
                return false;
            }
        });
        return $(winner);
    }

    function bindEventsToModal($modal) {
        var data_steps = [];
        $('[data-step]').each(function() {
            var step = $(this).data().step;
            if (step && $.inArray(step, data_steps) === -1) {
                data_steps.push(step);
            }
        });

        $.each(data_steps, function(i, v) {
            $modal.on('next.m.' + v, {step: v}, function(e) {
                goToStep(e.data.step);
            });
        });
    }

    function initialize() {
        reset();
        updateProgress(1, total_num_steps);
        $modal.find('.step-1').show();
        $progress_complete.hide();
        $progress_total.text(total_num_steps);
        bindEventsToModal($modal, total_num_steps);
        $modal.data({
            total_num_steps: $bodies.length,
        });
        if (reset_on_close){
            //Bootstrap 2.3.2
            $modal.on('hidden', function () {
                reset();
                $modal.find('.step-1').show();
            })
            //Bootstrap 3
            $modal.on('hidden.bs.modal', function () {
                reset();
                $modal.find('.step-1').show();
            })
        }
    }

    initialize();
})
}(jQuery);
Dmitry
  • 6,716
  • 14
  • 37
  • 39
Init7
  • 1
  • 1
  • 1
    Can you create a [mcve]? I tried to run your code on jsfiddle, but something I noticed is that you're selecting `.modal.multi-step` but your HTML doesn't contain any element with class `multi-step`. Is this your own code? Or did you copy it from elsewhere? Because it uses Bootstrap, but your question sounds like you didn't include bootstrap.css –  Dec 18 '17 at 11:20
  • I included bootstrap, the code is half mine, because I don't know JS. My boss ask me to use JS but I never used it because.. I hate it. – Init7 Dec 18 '17 at 11:23
  • How I can include multi-step in HTML? Sorry for the dumb question. I really never used js, even if I'm a developer. – Init7 Dec 18 '17 at 11:25
  • Look at what's basically the first line: `var modals = $('.modal.multi-step');` That line uses jQuery to select any HTML element with `class="modal multi-step"`, i.e. the actual modals shown to the user. Which means `modals` comes back empty, and all that code does essentially nothing. I also can't see any progress bar. It's like the JS code is written for a completely different HTML setup. –  Dec 18 '17 at 11:39

0 Answers0