/    Sign up×
Community /Pin to ProfileBookmark

Get the nearest
parent containing an child with class “sample”? jQuery

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

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@cootheadDec 10.2021 — Hi there pstein,

bearing in mind that it is not permissible for the

[b]p element[/b] to contain [b]block element[/b] this is one

possible solution...
&lt;!DOCTYPE HTML&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"&gt;

&lt;title&gt;Untitled document&lt;/title&gt;

&lt;!--
The internal CSS should be transferred to an external file
&lt;link rel="stylesheet" href="screen.css" media="screen"&gt;
--&gt;

&lt;style media="screen"&gt;
body {
background-color: #f0f0f0;
font: normal 1em / 1.5em sans-serif;
}
div, p, span {
padding: 1em;
margin: 1em;
border: 1px solid #000;
}
p{
border-color: #f00;
}
span{
display: inline-block;
border-color: #00f;
}
img {
background-color: #f0f;
}
&lt;/style&gt;

&lt;/head&gt;

&lt;body&gt;
&lt;div&gt; "1"
&lt;div&gt; "2"
&lt;p&gt;
&lt;div&gt; "3" &lt;!-- This is not a permissible child of a p element --&gt;
&lt;span&gt;
&lt;img class="sample" src="some.png" alt="some"&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;script&gt;
(function(d) {
'use strict';
var img = d.querySelector('img[class="sample"]'),
div = img.parentNode.parentNode;

<i> </i> console.log( div.textContent );
<i> </i>
}(document));
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;


The [b]p element[/b] would be ignored as the

[b]great grandparent[/b] of the [b]img element[/b]

and should be removed or replaced with

something more appropriate.

[i]coothead[/i]
Copy linkTweet thisAlerts:
@daveyerwinDec 10.2021 — > @pstein#1640391 If possible as jQuery statement.

``<i>
</i>&lt;div&gt; "1"
&lt;div&gt; "2"
&lt;p&gt;
&lt;div&gt; "3"
&lt;span&gt;
&lt;img class="sample"&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;script&gt;
var a=$('.sample').parent();
while(a.prop('tagName')!='DIV')a=a.parent();
a.css("color", "red");
&lt;/script&gt;<i>
</i>
``
Copy linkTweet thisAlerts:
@SempervivumDec 11.2021 — My version:
``<i>
</i> // vanilla js:
const nearest = document.querySelector('.sample').closest('div');
console.log(nearest.textContent);

// jQuery:
const nearest2 = $('.sample').closest('div');
console.log(nearest2.text());<i>
</i>
``

The function closest does what it's name is indicating: It finds the closest or nearest parent whose selector is matching.
×

Success!

Help @pstein spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...