I want to create a multistep form for the html structure below.
Condition: IF radio "Yes" is checked, then show "Section2" when clicked next. IF radio "No" is checked, then show "Section3" when clicked next button.
<form>
<!-- Section 1 -->
<div class="form-section current" id="section1">
<h2>Do you like fruits?</h2>
<label>
<input type="radio" name="likeFruits" value="yes"> Yes
</label>
<label>
<input type="radio" name="likeFruits" value="no"> No
</label>
<div class="form-button">
<button type="button" onclick="nextSection()">Next</button>
</div>
</div>
<!-- Section 2 -->
<div class="form-section" id="section2">
<h2>Which fruit do you like?</h2>
<label>
<input type="radio" name="favoriteFruit" value="apple"> Apple
</label>
<label>
<input type="radio" name="favoriteFruit" value="grape"> Grape
</label>
<div class="form-button">
<button type="button" onclick="prevSection()">Back</button>
<button type="button" onclick="nextSection()">Next</button>
</div>
</div>
<!-- Section 3 -->
<div class="form-section" id="section3">
<h2>Why don't you like fruit?</h2>
<label>
<input type="text" name="reason" placeholder="Enter reason">
</label>
<div class="form-button">
<button type="button" onclick="prevSection()">Back</button>
<button type="submit">Submit</button>
</div>
</div>
</form>
2
Answers
Well you need to handle it with nextSection() function, you can add this:
And don’t forget to give them a default hidden value with css:
Use the jquery on change function like this.