Return to the initial stage of a multistep process in its simplest form following a setTimeout delay

I recently customized the stepsForm.js by Copdrops and made some modifications. Although everything works well, I'm struggling to navigate back to the initial step (first question) after submitting the form due to my limited knowledge of JavaScript.

The original Copdrops script can be found here:

https://github.com/codrops/MinimalForm

Additionally, I implemented a modification to reset the form and return to the first question (step) post submission using the following link:

http://codepen.io/anon/pen/GgdQgg

Here is the custom JavaScript code snippet that I applied at the end of the Codepen's JavaScript:

var theForm = document.getElementById( 'theForm' );

new stepsForm( theForm, {
    onSubmit : function( form ) {
        // hide form
        classie.addClass( theForm.querySelector( '.simform-inner' ), 'hide' );
        var messageEl = theForm.querySelector( '.final-message' );
        messageEl.innerHTML = 'Thank you! We\'ll be in touch.';
        classie.addClass( messageEl, 'show' );

        setTimeout(function(){
                form.reset()
            }, 2500);
        setTimeout(function(){
            classie.removeClass( messageEl, 'show' )
        }, 3000);
        setTimeout(function(){
            classie.removeClass( theForm.querySelector( '.simform-inner' ), 'hide' )
        }, 3500);
    }
} );

Answer №1

JSBIN DEMO

// This function is responsible for submitting the form
    stepsForm.prototype._submit = function() {
        this.options.onSubmit( this.el );
        // Resetting the form after submission
        var self = this;
        setTimeout(function() {
            self.current = 0;
            self.isFilled = false;
            self._init();
            self._progress();
        }, 3500);
    }

Insert the following code at the end of the setTimeout function:

setTimeout(function(){
    classie.removeClass( theForm.querySelector( '.simform-inner' ), 'hide' );
    $("ol.questions li").removeClass("current");
}, 3500);

Answer №2

It appears that Afshin has pinpointed the issue accurately. The problem is arising from a validation error occurring before even inputting any values on the second turn. Upon further investigation, I discovered that this was due to the self._init(); method being invoked within the onSubmit() function, subsequently triggering the bindEvents() function as well. This inadvertently leads to duplicated event bindings, resulting in an increment of this.current variable twice while navigating through the form. To rectify this, a new function such as _reinit() should be implemented, mirroring the _init() function but excluding the bindEvents() call within it.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Troubleshooting: Why is my Bootstrap modal window only printing a

I've encountered an issue with printing the content of a Bootstrap modal window. Previously, the code I had was working fine but now it seems to be malfunctioning. Content is dynamically added to the form using appendChild() in a separate function. Ho ...

Locating the Active Object's Coordinates in fabric.js

When using fabric js, I have successfully drawn various shapes like circles and rectangles. However, I encountered an issue while trying to obtain the coordinates of the rectangle using the fabric.rect() method. canvas.getActiveObject().get('points& ...

The jQuery function is only operational upon page reload

I encountered an issue with a simple code snippet that I am using in a jQuery Mobile page on a multi-page website. The code, which is loaded from a separate file, was working initially but now only works when I reload the page. Upon inspecting the page loa ...

Tips for aligning placeholder and text at the center in a React Material UI TextField

Existing Layout: https://i.stack.imgur.com/Zv4Tg.png Desired Layout: https://i.stack.imgur.com/Xuj6O.png The TextField element is displayed as follows: <TextField multiline={false} autoFocus placeholder={props.defaultAmt} ...

How can a full-screen background image be created in Next.js?

To achieve a full height display in both the html and body tags, how can we do this within a next.js component? Check out [1]: https://i.stack.imgur.com/K1r9l.png [2] Visit: https://www.w3schools.com/howto/howto_css_full_page.asp "mainImage.jsx" import ...

What is the best way to convert an array of data into a dataset format in React Native?

Within my specific use case, I am seeking to reform the array structure prior to loading it into a line chart. In this context, the props received are as follows: const data = [26.727, 26.952, 12.132, 25.933, 12.151, 28.492, 12.134, 26.191] The objective ...

What is the best way to display numerical data within an inline list format?

Here is a list I have: <ol> <li>Login</li> <li>Address</li> <li>Shipping</li> </ol> The numbers in the list display correctly as decimals. However, when I change the <li> tag to inline or ...

Error message: Electron is unable to read properties of undefined, specifically the property 'receive'. Furthermore, the IPC is unable to receive arguments that were sent through an HTML iframe

I am currently working on passing light mode and language data from ipcMain to ipcRenderer via my preload script: Preload.js: const { contextBridge, ipcRenderer } = require("electron"); const ipc = { render: { send: ["mainMenuUpdate& ...

Assign value to an element in an array using the value from another element

Is it possible in Javascript to set the value of one array element based on another without changing both elements? Specifically, how can I only modify arr[1] while leaving other elements unchanged? arr[0] = {i: 0}; arr[1] = arr[0]; arr[1]['summ&apos ...

Wrap the json response in HTML elements

I'm currently learning about AJAX, and I've encountered a major issue that I can't seem to resolve. I pass several variables to PHP, perform various checks there, and then return string values to AJAX. Strangely, I am able to display these r ...

Ways to create a new line in JSX using a JavaScript string

My JSX contains a string that I utilize: const message = 'Hello World'; and in my JSX: <p>{message}</p> Is there a way to start a new line from World? The desired output is: Hello World ...

Simply output the integer value

Recently, I've been working on a function that I'm trying to condense into a one-liner code for a challenge on Codewars. You can find the problem here. Here's the code snippet that I currently have: export class G964 { public static dig ...

What is the best way to design a footer that remains fixed at the bottom of the screen but becomes unfixed when the user scrolls down?

Currently, I am working on creating a footer that remains fixed at the bottom of the user's screen regardless of the screen size. However, I also want the header to transition from a fixed position to being part of the page when the user scrolls down. ...

Placing the input-group-addon above the text-input for better positioning

Feeling overwhelmed trying to finalize a simple form due to CSS issues? Check out this image for a visual of the problem: https://i.stack.imgur.com/PgCmN.jpg The label is slightly off to the left, and the button appears larger than the input field. The i ...

Leveraging underscore.js for null verification

let name = "someName"; if(name !== null) { // perform some action } Currently, I am utilizing http://underscorejs.org/#isNull. How can I achieve the same functionality using underscore.js? Is there any noticeable performance enhance ...

Is it possible for jQuery to retrieve numerical values in a similar way to PHP's (int) function

Is there a method in jQuery that can filter out only numbers, similar to how PHP's (int) function works for getting numbers? For example, if I have text that includes both strings and numbers: <div class='selector'>(4)</div> $ ...

What is the best way to pass only the second parameter to a function in TypeScript?

Let's consider a TypeScript function as shown below: openMultipleAddFormModal(commission?: Commission, people?: People): void { // some data } To make a parameter optional, I have added the Optional Chaining operator. Now, how can I modify the code ...

Utilizing NPM Package Configuration Variables with Docker Commands: A Guide

I have a unique file structure where my package.json contains a single variable called settings which defines the port for the application: package.json ... "settings":{ "port": "3000" }, ... In addition, I've set up a custom script to execute a ...

Methods for enlarging a sub-element without any impact on its encompassing parent element

I need the child class to not affect the overflow of the parent when I hover over it. My initial thought was to remove the line position: relative; from the parent, but this solution does not work properly when dealing with multiple nested positions. .p ...

option for uploading various data to the database using (php, ajax)

I am using a foreach loop to display data from the database on the screen. However, when I click the button, I do not see any results. Can someone please help me identify where I may be making mistakes? <table class="table"> <thead class="the ...