[ACCEPTED]-get the ID of last DIV inside a DIV using jQuery-html

Accepted answer
Score: 26

You can try this:

jQuery Code

$(document).ready(function(){
   $('#container').children().last().attr('id');
});

0

Score: 13

Try this:

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

0

Score: 6

Well you can do this:

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

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

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

Score: 3

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

You can then 1 get the id

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

See demo.

More on jquery .last()

More Related questions