Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

How do you create a multi-page form?

Hi!

I am trying to create a multipage form. (I have completed the web design and some of the full stack developer track)

I'm just wondering the best way to do this: - create separate HTML files for each page? - cycle through different fieldsets somehow? - ...?

This is the website right now bit.ly/Formdemo I'll give it to the backend dev once I'm done!

Thanks so much! I've looked everywhere online and can't find an answer

3 Answers

You might want to take a look at some of the websites that go through the steps, like these:

There are some suggestions on this one that are interesting, especially the progress stuff:

I suggest you try out several of these methods. As you will see, there are a variety of approaches, and the one you like may be a combination. Some depend on SESSION variables, some on hiding and showing the form parts, some on hidden fields. You'll find that different developers have strong feelings about some of this stuff, especially SESSION variables!

The bottom line, from my point of view, is ease of use for the user. You want to make filling in the form as "enjoyable" as possible. Some nicer features are allowing them to fill in a part and save that part and then come back to finish the form, especially if it's quite long, like some application forms. Another nice feature is to give them a Back button so they can review entries on an earlier page. Some multi-page forms combine the progress bar with the Back button by showing the steps and making the name of each step a link. A really critical feature is that if they go back they see the entries they made. And if they make a mistake in a field (e.g., don't pass validation for an email) they don't lose all the other entries they have already made.

Matt F.
Matt F.
9,518 Points

Hi James,

Is it important to you that the form is actually on two different pages? Or do you only care that it is perceived as multiple pages?

Single page applications, using Angular/React or even just plain JavaScript are single pages that use JavaScript to give the perception that the users are going to separate pages.

If you are just looking for next/prev buttons to fill in small chunks of data at a time - then just show and hide the certain form elements depending on the current state/page the current user is on, then this is probably what you are looking for.

For examples, I could have an object of arrays that would hide/show the elements based on the current page. This is just a barebones example that would need additional state tracking for a previous button and additional pages - but this is a basic implementation in plain JavaScript:

var pageContents = [
  ['#name', '#email'],
  ['#phone', '#age']
];

function classAssigner(element, classNameParam) {
  //finds the element and assigns it the proper class, or an empty string
  var curElement = document.querySelector(element);
  curElement.className = classNameParam;
}

//this function gets called dueto the onclick attribute in the html on the button
function pageChanger(curPage, nextPage, pageContentArray) {
  //calls classAssigner for each item in the curPage index of the pageContent array
  pageContentArray[curPage].forEach(function(element) {
    classAssigner(element, 'hidden')
  });

  //calls classAssigner for each item in the nextPage index of the pageContent array
  pageContentArray[nextPage].forEach(function(element) {
    classAssigner(element, '')
  });
}
<input type="text" id="name" placeholder="name" />
<input type="text" id="email" placeholder="email address" />

<input type="text" id="phone" class="hidden" placeholder="phone number" />
<input type="text" id="age" class="hidden" placeholder="age" />

<button onclick="pageChanger(0, 1, pageContents)" id="next">next page</button>
.hidden {
  display: none;
}

Here is a link to the same code working on Code Pen: http://codepen.io/anon/pen/EKPaQe?editors=0100

Thank you both for your answers!

I used the Code Pen example concept and took my own spin on it:

// hide all fieldsets after 1st
$('fieldset').slice(1).hide();

var pageNumber = 1;

// when next is clicked
$('.mdl-button').click(function () {

    console.log('Clicked');
    console.log(pageNumber);

    // hide current page
    $('form>:nth-child(' + pageNumber + ')').hide();

    pageNumber += 1;

    // show next page
    $('form>:nth-child(' + pageNumber + ')').show();
});