pstein Posted December 10, 2021 Share Posted December 10, 2021 Assume I have the following simplified code: <div> "1" <div> "2" <p> <div> "3" <span> <img class="sample">...</img> </span> </div> </p> </div> </div> Now I want to refer to innermost parent <div> element which contains a child element with class "sample". Here it is an <img> element in other cases it could be a <figure> or whatever. As target <div> element I would like to get the <div> labelled here with "3". How can I achieve this? If possible as jQuery statement. Thank you Link to comment Share on other sites More sharing options...
Ingolme Posted December 10, 2021 Share Posted December 10, 2021 Something likeĀ $("img.sample").parent().parent() should work. Link to comment Share on other sites More sharing options...
dsonesuk Posted December 10, 2021 Share Posted December 10, 2021 (edited) $("img.sample").closest('div') or maybe $("img.sample").parents('div').eq(0) or $("img.sample").parentsUntil("div") removing element type 'img' will target any element using class .sample Edited December 10, 2021 by dsonesuk 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now