Multi step form with jquery issues - navigation buttons are not working
Im trying to use the jquery steps plugin to have a multi step form. I just copy the available example code on the documentation and made some html and css changes and the form is working now like this: "
https://jsfiddle.net/hyuzhytj/5/".
But it has 3 issues:
- The navigation buttons next and previous don't work.
- In the first step, there are many form fields and the last fields, for example, the field with id="notes" don't appear, it seems that it's hidden.
- I'm also using the select2 plugin in this page and in this field it is appearing a small rectangle in the input and I'm not understanding why.
Do you know how to fix this?
Note: With this plugin, each step title is inside a `<h3>` element and the form fields inside each step content are inside a `<fieldset>` elementa.
**HTML:**
- <div class="container nopadding py-4">
- <div class="row justify-content-center align-items-center">
- <div class="col-12">
- <h1 class="h5 text-center text-heading-blue font-weight-bold">Title</h1>
- </div>
- </div>
-
- <div class="row mt-3 d-flex justify-content-center">
- <div class="col-12">
- <form id="example-advanced-form" action="#">
- <h3 class="d-none d-md-block">Step 1</h3>
- <fieldset>
- <div class="form-group">
- <label for="conference_name" class="text-heading h6 font-weight-semi-bold">Conference Name *</label>
- <input class="form-control" id="conference_name" name="conference_name" type="text" class="required">
- </div>
-
- <div class="form-group col-md-6 px-0">
- <label for="conference_categories" class="text-heading h6 font-weight-semi-bold">Conference categories *</label>
- <select id="tag_list" multiple class="form-control required" name="conference_categories" id="conference_categories">
- <option>category</option>
- <option>category2</option>
- </select>
- </div>
-
- <div class="form-group">
- <label class="text-heading h6 font-weight-semi-bold">Address</label>
- <div class="form-row">
- <div class="col">
- <input type="text" name="conference_city" class="form-control required">
- </div>
- <div class="col">
- <input type="text" name="conference_zip_code" class="form-control required">
- </div>
- </div>
- </div>
- <div class="form-row">
- <div class="form-group col">
- <input type="text" required name="conference_address" class="form-control">
- </div>
- </div>
- <div class="form-row">
- <div class="form-group col">
- <input type="text" required name="conference_place" class="form-control">
- </div>
- </div>
-
- <div class="form-row">
- <div class="form-group col col-lg-6">
- <label for="conference_image" class="text-heading h6 font-weight-semi-bold">Conference Image</label>
- <label class="conference_image">
- <input type="file" required id="conference_image" name="conference_image" class="custom-file-input">
- <span class="custom-file-control"></span>
- </label>
- </div>
- </div>
-
- <div class="form-group">
- <label for="conference_description" class="text-heading h6 font-weight-semi-bold">Description</label>
- <textarea class="form-control" id="textarea" name="conference_description" class="required"></textarea>
- </div>
-
- <div class="form-group">
- <label for="notes" class="text-heading h6 font-weight-semi-bold">Notes</label>
- <input class="form-control" id="notes" name="notes" type="text" class="required">
- </div>
- </fieldset>
-
- <h3>Step 2</h3>
- <fieldset>
- <p> Step 2 content</p>
- </fieldset>
-
- <h3>Step 3</h3>
- <fieldset>
- <p>Step 3 content</p>
- </fieldset>
-
- </form>
- </div>
- </div>
- </div>
**jquery**:
// select2
- $('#tag_list').select2({
- placeholder: '',
- dropdownAutoWidth: 'true',
- width: '100%'
- });
// jquery steps
- var form = $("#example-advanced-form").show();
-
- form.steps({
- headerTag: "h3",
- bodyTag: "fieldset",
- transitionEffect: "slideLeft",
- onStepChanging: function (event, currentIndex, newIndex)
- {
- // Allways allow previous action even if the current form is not valid!
- if (currentIndex > newIndex)
- {
- return true;
- }
- // Forbid next action on "Warning" step if the user is to young
- if (newIndex === 3 && Number($("#age-2").val()) < 18)
- {
- return false;
- }
- // Needed in some cases if the user went back (clean up)
- if (currentIndex < newIndex)
- {
- // To remove error styles
- form.find(".body:eq(" + newIndex + ") label.error").remove();
- form.find(".body:eq(" + newIndex + ") .error").removeClass("error");
- }
- form.validate().settings.ignore = ":disabled,:hidden";
- return form.valid();
- },
- onStepChanged: function (event, currentIndex, priorIndex)
- {
- // Used to skip the "Warning" step if the user is old enough.
- if (currentIndex === 2 && Number($("#age-2").val()) >= 18)
- {
- form.steps("next");
- }
- // Used to skip the "Warning" step if the user is old enough and wants to the previous step.
- if (currentIndex === 2 && priorIndex === 3)
- {
- form.steps("previous");
- }
- },
- onFinishing: function (event, currentIndex)
- {
- form.validate().settings.ignore = ":disabled";
- return form.valid();
- },
- onFinished: function (event, currentIndex)
- {
- alert("Submitted!");
- }
- }).validate({
- errorPlacement: function errorPlacement(error, element) { element.before(error); },
- rules: {
- confirm: {
- equalTo: "#password-2"
- }
- }
- });
CSS:
The css is a bit large, so I just put it in the fiddle, first there is the css for the jquery steps, and then the css for the select2 plugin.