Ok so I feel like I'm missing something let me show you a very simple function I wrote.
function findClosestAncestor(html_child_element, html_class_name) {
if (!html_child_element.parentElement.classList.contains(html_class_name)) {
findClosestAncestor(html_child_element.parentElement, html_class_name)
} else {
return html_child_element.parentElement;
}
}
When I am feeding in a child element from my page I debug this function and as expected it hits the else statement after running through twice. This has been confirmed via Chrome's dev tools debugging it live and is the element on the page I wish to return. However when the function is called and set to a variable like so
let parentPanel = findClosestAncestor(e.target, "panel");
parentPanel is undefined after I exit the function. This is causing me a bit of confusion and I may just be overlooking something very simple here. What is happening.
EDIT: Per Thomas suggestion I have tested .closest() this still is not returning a value either. The relevant parts of the structure of the HTML is below
<div class="list-info">
<div class="container">
<div class="account-type text-title">Type</div>
<div class="account-type-placeholder text-info">Type Placeholder</div>
</div>
<div class="container">
<div class="account-phone text-title">Phone</div>
<div class="account-phone-placeholder text-info">Phone Placeholder</div>
</div>
<hr /> <hr />
<button class="sublist-accordion">
<div class="sublist-title">
Other cases for this Customer
</div>
</button>
<div class="sublist-panel">
The element we are calling either method on is the final sublist-panel you see the top of there. It should be and in the case of my method is successfully finding the panel class above. However it is still not allowing me to assign that out as a return value and is returning undefined for both my method and .closest
returnin yourifblocklet parentPanel = e.target.closest(".panel");MDN Element#closest()