Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

last element with a class name in a div

Tags:

jquery

how can i get the last div with class a in a div the id = test ? in this case i have to get the div with content = 1000

<div id="test">
<div class="a">1</div>
..
..
<div class="a>1000</div>
</div>
like image 800
Luca Romagnoli Avatar asked Jan 23 '10 13:01

Luca Romagnoli


People also ask

How do I get the last element of a class in CSS?

The first <div/> element with class milestone can be selected with the :first-of-type selector and the last <div/> element with class milestone can be selected with the :last-of-type selector. We use the above two selectors to round off the borders of the first and last milestones.

How do I find my last div id?

You can use the . last() method which will help you to match the last element. $('#container div'). last().


3 Answers

You can use the :last pseudo-selector:

$('#test div.a:last')
like image 83
Tom Avatar answered Oct 05 '22 09:10

Tom


Without jQuery:

var divs = document.getElementById("test").getElementsByTagName("div");
var lastChild = divs[divs.length - 1];
like image 40
Joel A. Villarreal Bertoldi Avatar answered Oct 05 '22 08:10

Joel A. Villarreal Bertoldi


using the proposed at

const nodesHighlighted = document.querySelectorAll('.day--highlight');
const lastNodeHighlighted = [...nodesHighlighted].at(-1);

at the current writing it's not supported by safari

like image 36
PaulWanjohi Avatar answered Oct 05 '22 09:10

PaulWanjohi