Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append text after last css class

I have the following html

<div class="someClass">  
    Some text  
</div>  
<div class="someClass">  
    Some other text  
</div>
<div class="add>
    <img src="someImage.jpg">
</div>

Now I'd like to append another div after a click on the add image - but only after the last div with the class "someClass".

If I use the following jQuery it will be appended after each someClass element, so multiple times.

$(".add img").live("click", function() {
    $(".containerFooter").after("<div class='someClass'>test</div>");
});  

Is there a way to only append it after the last div with the someClass attribute?

like image 898
jrn Avatar asked Dec 21 '22 20:12

jrn


2 Answers

You are looking for the :last selector:

$(".someClass:last").after("<div class='someClass'>test</div>");
like image 64
Josh Mein Avatar answered Jan 03 '23 15:01

Josh Mein


$('.someClass').last() 

OR

$('.someClass:last')

will give you last element of class someclass

like image 44
Rajat Singhal Avatar answered Jan 03 '23 15:01

Rajat Singhal