Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the ID of last DIV inside a DIV using jQuery

Tags:

html

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>
</div>

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

like image 943
brian wilson Avatar asked Aug 25 '12 07:08

brian wilson


2 Answers

You can try this:

jQuery Code

$(document).ready(function(){
   $('#container').children().last().attr('id');
});
like image 199
Codegiant Avatar answered Oct 22 '22 07:10

Codegiant


Try this:

var id = $('#container div:last').attr('id')
like image 38
undefined Avatar answered Oct 22 '22 08:10

undefined