Recently, I've been developing a form for my website that involves implementing some slide-down functions. In this setup, there are 3 divs and jQuery code to dynamically add or remove these divs as needed.
- Upon clicking a button for the first time, the corresponding div expands, revealing a "remove" button while replacing the original button with another that performs the same function for a different div with a unique ID. This sequence repeats three times.
- If a user opts to click "remove", the div is removed and the "add another" button is swapped back to its previous state. However, things get tricky if a user decides to remove a div out of order, leading to duplicated buttons that don't serve any purpose.
The structure of the code looks like this:
<div class="form-group">
<h2 class="text-center">Vehicle Info</h2>
<label for="inputEmail3" class="col-sm-2 control-label">Vehicle:</label>
<div class="col-sm-10">
<div class="col-sm-4">
<input type="email" class="form-control" id="inputEmail3" placeholder="Year">
</div>
<div class="col-sm-4">
<input type="email" class="form-control" id="inputEmail3" placeholder="Make">
</div>
<div class="col-sm-4">
<input type="email" class="form-control" id="inputEmail3" placeholder="Model">
</div>
</div>
</div>
... (additional form groups)
For the full HTML markup, check out the JSFiddle link provided at the end of this post.
As for the jQuery implementation:
$(document).click(function(){
// Handling show/hide logic for each element
});
Despite my attempt to create an if loop in my jQuery script, I encountered difficulties reaching the desired outcome. What I aim to achieve is having the "add another" button trigger multiple actions, specifically duplicating a similar div up to 3 times with distinct IDs. This way, I can collect accurate data via PHP when processing the form submissions sent to my email.
If you need further clarification or have suggestions on how to enhance this functionality, please feel free to share your insights. Your feedback is highly appreciated!
To view the live output, visit this JSFiddle link.