This is a simple HTML structure of a multi-step form. I want to use the first name of user as personalisation in form section <p>
.
For say if someone enters their name as "Jack" and then clicks on next button…. then in the next section the <p>
will read as "Jack, What’s your age?" and then the final section’s <p>
will be "Are you good Jack?"
<form action="">
<div class="section1">
<input type="text" placeholder="First Name">
<button class="next-btn">Next</button>
</div>
<br>
<div class="section2">
<p>[First Name], What's your age?</p>
<input type="number" class="age" placeholder="Your Age">
<button class="next-btn">Next</button>
</div>
<br>
<div class="section3">
<p>Are you doing good [First Name]?</p>
<input type="text" class="good" placeholder="Yes or No ?">
<button class="submit">submit</button>
</div>
</form>
I am new to JS. So am not able to understand how to do this. Looking forward to some assistance, please.
2
Answers
I would do it like this, i hope it helps you!
To use the first name in later stages of the form, create empty
<span>
s whose.textContent
can be edited with JS.Then, to get multistage functionality, you can create a CSS utility class
hidden
to hide everything except the first stage. Then, you can add event handlers for each of the "Next" buttons that will stop the default event (which is submitting the form), and optionally do some stuff (like setting some<span>
‘s.textContent
with the value of an input), then toggle off thehidden
class on the next section to show it. Like this: