Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check what div span is placed in

Tags:

html

jquery

I am trying to find out what div is the span is in using jquery.

here is a eg.

 <div id='one'></div>
 <div id='two'></div>
 <div id='three'><span id="find_me"></span></div>

thanks

like image 617
Rickstar Avatar asked Feb 27 '23 02:02

Rickstar


2 Answers

Use .closest() to find the closest ancestor div (.parent() will only find it it if the div is the immediate parent).

$('#find_me').closest('div').attr('id')
like image 192
lonesomeday Avatar answered Mar 03 '23 09:03

lonesomeday


alert ( $("#find_me").parent().attr("id") ); // <- three

like image 20
25 revs, 4 users 83% Avatar answered Mar 03 '23 09:03

25 revs, 4 users 83%