How would you write the Jquery to get the closest div that actually has an ID defined?
JQuery: Closest div that has an ID
You should use has attribute selector. This sample should do the work:
$('selector').closest('[id]')
This looks to ancestors only. If he wants a sibling or child that's closest as well this won't be enough. –
Lepus
how does this give me divs with ids as opposed that anything with an id? –
Marbling
Don't you mean
.closest('div[id]')
? –
Septuplet $(elementToStart).parent().closest('div[id]');
I use the parent() to avoid just getting the element itself.
Example: http://jsfiddle.net/zQRFT/1/
this should be the answer, without using parent() mine was not working –
She
parent().closest() is the same as using parents() which begins with the parent element –
Sirreverence
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
© 2022 - 2024 — McMap. All rights reserved.
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. – Lepus