Struggling to iterate through a stack of HTML Elements, I attempted to write a recursive function with no success.
In the code snippet below, I'm facing challenges in returning a value from an if statement and ultimately from the function itself.
While I can successfully console.log the necessary information, I am unable to return it. This issue is uncommon for me, prompting me to seek fresh eyes on this code revision.
function findElementByDataValue(target: EventTarget, data: {key: string, value: string}){
if (target.dataset[data.key] === data.value) {
return target;
};
if (target.children.length > 0) {
for (const child in target.children) {
const element = target.children[child];
// Attempts to return within the "recursive" function seem to cause abrupt execution
if (element.children && typeof element === 'object') {
findElementByDataValue(element, data);
}
}
}
}
If you have any insights or spot an issue with my recursive function, your help would be greatly appreciated.