How would you write the Jquery to get the closest div that actually has an ID defined?
3 Answers
You should use has attribute selector. This sample should do the work:
$('selector').closest('[id]')
$(elementToStart).parent().closest('div[id]');
I use the parent() to avoid just getting the element itself.
Example: http://jsfiddle.net/zQRFT/1/
Look for an id attribute on a div, using the closest method:
$(this).closest('div[id]');
The [id]
brackets there is what's called the Has Attribute Selector
closest
method being available, the answer to this would be at least 50-100 lines of code if you want to search siblings and children as well.