Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: Closest div that has an ID

Tags:

jquery

closest

How would you write the Jquery to get the closest div that actually has an ID defined?

like image 649
mike Avatar asked Sep 11 '25 09:09

mike


2 Answers

You should use has attribute selector. This sample should do the work:

$('selector').closest('[id]')
like image 69
gor Avatar answered Sep 12 '25 21:09

gor


$(elementToStart).parent().closest('div[id]');

I use the parent() to avoid just getting the element itself.

Example: http://jsfiddle.net/zQRFT/1/

like image 37
AlfaTeK Avatar answered Sep 12 '25 23:09

AlfaTeK