Find Parent Divs
April 8, 2025 · AI Conversations
User
hover-text-box-container find each parent div under this
Assistant
To find each parent “ under the element with class `.hover-text-box-container` using JavaScript or jQuery, here's how you can do it:
### ✅ Using **JavaScript**:
const container = document.querySelector('.hover-text-box-container');
const childDivs = container.querySelectorAll(':scope > div'); // direct child divs only
childDivs.forEach((div, index) => {
console.log(`Child div ${index + 1}:`, div);
});
If you want **all descendant “ elements** (not just direct children):
const allDivs = document.querySelectorAll('.hover-text-box-container div');
allDivs.forEach((div, index) => {
console.log(`Div ${index + 1}:`, div);
});
—
### ✅ Using **jQuery**:
// Direct children only
$('.hover-text-box-container > div').each(function(index) {
console.log('Child div', index + 1, this);
});
// All nested divs
$('.hover-text-box-container div').each(function(index) {
console.log('Nested div', index + 1, this);
});
Let me know what exactly you're trying to achieve — for example, filter only visible divs, or ones with a specific class — and I can tailor the selector further.