Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
147
rated 0 times [  150] [ 3]  / answers: 1 / hits: 61031  / 10 Years ago, wed, november 19, 2014, 12:00:00

I want to add elements after the last one.
My current code



<div class=find><div id=FirstElement> /*First element loaded first */ </div><div>

$('#AddNextElement' + id).click(function (e) {
$('<div id=NextElement' + id +'>' + /*Add after the last element*/ + '</div>').insertAfter($(#FirstElement));
}


Current it adds only it after the first element:




1
4
3
2




I want it to add after the last element every time:




1
2
3
4




I've followed these links and I didn't find what I'm looking for:



jQuery insertAfter last item



insertAfter specific element based on ID



jQuery: Add element after another element



Thank you in advance!.



How I fixed it:



$('#AddNextElement' + id).click(function (e) {
$('<div id=NextElement' + id +'>' + /*Add after the last element*/ + '</div>').insertAfter($(#FirstElement).parent().find('.Finder').last());
}


I found the .parent().find('.find').last(); then insert after the last


More From » jquery

 Answers
9

Just you need last() method



$('<div id=NextElement' + id +'>' + /*Add after the last element*/ + '</div>')
.insertAfter($('[id^=NextElement]').last());

[#68766] Saturday, November 15, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kelsy

Total Points: 486
Total Questions: 86
Total Answers: 76

Location: El Salvador
Member since Sun, Sep 12, 2021
3 Years ago
kelsy questions
Tue, Oct 19, 21, 00:00, 3 Years ago
Tue, Aug 4, 20, 00:00, 4 Years ago
Wed, Jun 17, 20, 00:00, 4 Years ago
Fri, Jan 10, 20, 00:00, 4 Years ago
;