function findRelatedElement(startElementId, relationship, selector = null) { const startElement = document.getElementById(startElementId); if (!startElement) { console.error(`Starting element with ID '${startElementId}' not found.`); return null; } switch (relationship) { case 'parent': return startElement.parentElement; case 'closest': if (!selector) { console.error('Selector is required for 'closest' relationship.'); return null; } return startElement.closest(selector); case 'children': // Returns a live HTMLCollection return Array.from(startElement.children); case 'first-child': return startElement.firstElementChild; case 'last-child': return startElement.lastElementChild; case 'next-sibling': return startElement.nextElementSibling; case 'prev-sibling': return startElement.previousElementSibling; default: console.warn('Invalid relationship. Use parent, closest, children, first-child, last-child, next-sibling, or prev-sibling.'); return null; } } // Example usage: //