Reputation: 803
User enters pairs of values. I need to replicate the last set and renumber their ids. The function below runs, but the new div doesn't appear. I'm off in the weeds somewhere... thanks for help!
$('#addparm').click(function (event) {
event.preventDefault();
var maxSeq = $('#maxseq').attr("seq");
var lastDiv = $('#pct[' + maxSeq + ']').parent();
$('#maxseq').attr("seq", ++maxSeq);
var newDiv = $(lastDiv).clone(true);
$(lastDiv).after(newDiv);
$(newDiv).fadeIn('slow');
});
here's the HTML:
...
<div id="bkp[8]">
<input seq="8" id="pct[8]" name="pct" type="text" value="0.28" />
<input seq="8" id="amt[8]" name="amt" type="text" value="43.2" />
<input seq="8" id="use[8]" name="use" type="hidden" value="yes" />
</div>
<div id="bkp[9]">
<input seq="9" id="pct[9]" name="pct" type="text" value="0.31" />
<input seq="9" id="amt[9]" name="amt" type="text" value="41.4" />
<input seq="9" id="use[9]" name="use" type="hidden" value="yes" />
</div>
<input type="hidden" id="maxseq" seq="9" />
<a id="addparm" href="javascript:void(0)">Add</a>
Upvotes: 0
Views: 154
Reputation: 19560
Clone is working fine, but your selector is not finding anything on which to operate.
Per the HTML specs, you cannot use square brackets in an element's ID. Further, your selectors which look for elements with IDs that have square brackets are not matching what you think they are.
The selector:
#foo[5]
Does not match an element with ID foo[5]
, rather it looks for an element with ID foo
and an attribute named 5
.
This means that lastDiv
is holding an empty jQuery collection, so nothing gets cloned and nothing gets appended.
The solution is to either escape the square brackets in your selectors while maintaining the improper ID attr values (not recommended), or modify your ID values to meet the specs and adjust your code to the new ID format.
Upvotes: 3