Luca Romagnoli
Luca Romagnoli

Reputation: 12455

last element with a class name in a div

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>

Upvotes: 36

Views: 69317

Answers (4)

Paul Wanjohi
Paul Wanjohi

Reputation: 119

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

Upvotes: 1

Tom
Tom

Reputation: 22841

You can use the :last pseudo-selector:

$('#test div.a:last')

Upvotes: 92

Without jQuery:

var divs = document.getElementById("test").getElementsByTagName("div");
var lastChild = divs[divs.length - 1];

Upvotes: 16

Reigel Gallarde
Reigel Gallarde

Reputation: 65264

$('div#test div:last-child');

Upvotes: 2

Related Questions