Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Parent ID of this DIV

I am looking to find the parent div id (i.e. "Bakerloo") from this layout when the button is clicked within ".buttonleft0" in jquery / javascript.

<div id='Bakerloo' class='box'>bakerloo<p></p><span class='buttons'>
<span class='buttonleft0'><button onClick='up()'><span class='icon icon10'></span>
</button>
</span><span class='buttonleft'></span><span class='buttonright'></span></span>
<div class='comingup'></div>
<div class='more'></div></div>

I have tried:

$(this).parent('id');

But this just returns 'undefined'.

like image 945
ldmo Avatar asked Dec 08 '22 17:12

ldmo


2 Answers

$(this).closest('div').attr('id')

I think this is what you want

like image 154
cosmin.danisor Avatar answered Dec 11 '22 10:12

cosmin.danisor


The parent() function returns the jQuery object, so you need to use the attr() for any of it's attributes like so:

$(this).closest().attr('id');

Edit: On further inspection it appears the button isn't the direct child of the div, and so use of the closest() function would be required.

like image 27
Tom Walters Avatar answered Dec 11 '22 10:12

Tom Walters