get the ID of last DIV inside a DIV using jQuery

I have the below.

<div id=container>
    <div id=box1></div>
    <div id=box2></div>
    <div id=box3></div>
    <div id=box4></div>
    <div id=box5></div>
    <div id=box6></div>

What is the correct way to get the id of the last div inside container using jQuery?


You can try this:

jQuery Code


Try this:

var id = $('#container div:last').attr('id')

Well you can do this:

$("#container div").last().attr("id")

Or if you only want to include divs that are direct children of #container (assuming your real-world code has more elements and might have divs inside the divs) change the above selector to "#container > div".

But note that there's no one "correct" way.

You can use the .last() method which will help you to match the last element.

You can then get the id

$('#container div').last().attr('id');

See demo.

More on jquery .last()

Need Your Help

Background task, progress dialog, orientation change - is there any 100% working solution?

android orientation android-orientation

I download some data from internet in background thread (I use AsyncTask) and display a progress dialog while downloading. Orientation changes, Activity is restarted and then my AsyncTask is comple...

Completely removing phpMyAdmin

php apache ubuntu phpmyadmin suexec

I installed virtualmin and phpmyadmin separately using the apt-get command. phpMyAdmin didn't work because of Virtualmin's security settings (suexec).