whiteletters in blankpapers whiteletters in blankpapers - 7 months ago 34
HTML Question

Get id of div in nested HTML elements using jquery

How can I get, using jquery, the

id
of the first div with class:
post
(367), in the following HTML code:

<div id="own-posts">
<span class="title">Me</span>
<div class="posts_container">
<div class="post"></div>

<div id="367" class="post"></div>
<div id="365" class="post"></div>
<div id="345" class="post"></div>
<div id="343" class="post"></div>

</div>

</div>


Among the solutions I tried:

$('#own-posts').children()[1].children()[1].attr('id');

Answer

The first .post element doesn't have an ID, so I guess you mean the first .post element that actually has an ID attribute :

var id = $('#own-posts .post[id]:first').prop('id');

FIDDLE