Reputation: 434
I know there are a lot of questions on Stack about this topic, but I cannot get my specific problem to work. I've looked at this question: jquery increment id and name of children within appended rows
And used the code provided there, but I can't get my example to work. I have a fiddle up here: https://jsfiddle.net/af3z0kx5/2/
using the following HTML:
<main>
<form class="application">
<h3>Work Experience</h3>
<p class="note">Note: We check all references. Click the (+) or (-) buttons below to add or remove previous employment records.</p>
<div class="work-experience">
<div class="section group full">
<div class="col span_6_of_12">
<label for="fromdate">From (start date)</label>
<input type="date" id="fromdate" name="From-Date" />
</div>
<div class="col span_6_of_12">
<label for="todate">To (start date)</label>
<input type="date" id="todate" name="To-Date" />
</div>
</div>
<label for="workexp-employer">Employer's Name</label>
<input type="text" name="Work-Exp-Employer" id="workexp-employer" />
<label for="workexp-employer-address">Employer's Address</label>
<input type="text" name="Work-Exp-Employer-Address" id="workexp-employer-address" placeholder="Street Address, State, and ZIP"/>
<label for="workexp-employers-phone">Employer's Phone</label>
<input type="tel" name="Work-Exp-Employer-Phone" id="workexp-employers-phone" pattern="\d{3}[\-]\d{3}[\-]\d{4}" placeholder="ex. 123-456-7890" />
<label for="workexp-duties">Your Duties</label>
<textarea placeholder="Summarize" class="duties"></textarea>
<label for="workexp-supervisor">Supervisor's Name</label>
<input type="text" name="Work-Exp-Supervisor" id="workexp-supervisor" />
<div class="section group full">
<div class="col span_6_of_12">
<label for="starting-salary">Starting Rate/Salary</label>
<input type="text" id="starting-salary" name="Starting-Salary" />
</div>
<div class="col span_6_of_12">
<label for="ending-salary">Ending Rate/Salary</label>
<input type="text" id="ending-salary" name="Ending-Salary" />
</div>
</div>
<label for="workexp-leaving">Specific Reason for Leaving</label>
<input type="text" name="Work-Exp-Leaving" id="workexp-leaving" />
<hr />
</div>
<div class="control">
<a href="#" class="add-work-exp"> </a>
<a href="#" class="remove-work-exp"> </a>
</div>
</form>
</main>
And the jQuery:
var i = $(".application .work-experience").length;
$(".add-work-exp").click(function(e) {
e.preventDefault();
var clonedRow = $(".work-experience").clone().find("input:text, textarea").val("").end();
i++;
$("*[name*='1']", clonedRow).attr('id', function() {
var attrval = $(this).attr("id");
attrval = attrval.replace(/\d/g, "");
while($('#' + attrval + i).size() > 0) {
i++;
}
return attrval + i;
}).attr('name', function() {
var attrval = $(this).attr("name");
attrval = attrval.replace(/\d/g, "");
return attrval + i;
});
$(".work-experience").append(clonedRow);
});
$(".remove-work-exp").click(function (e) {
e.preventDefault();
$("form .work-experience").last().remove();
});
The issues I am having with this code:
.work-experience
div. I need it to go below it and outside of it. (I tried using .insertAfter
instead of .append
and that made the whole div disappear for some reason.)Thank you for any help you can give. I am not that good at jQuery (obviously) but I need to get this solved.
Upvotes: 0
Views: 78
Reputation: 434
Big thank you to @Taplar for pointing me in the right direction.
I had 2 issues with my code:
size()
- this was causing an error. I changed it to length
and it worked!New fiddle: https://jsfiddle.net/32r8z6xs/
Upvotes: 1