Skip to content

Instantly share code, notes, and snippets.

View thedamon's full-sized avatar

Damon Muma thedamon

View GitHub Profile
@thedamon
thedamon / getSections.js
Last active August 31, 2022 14:18
returns the header of the section containing a given element
function getSectionHeader(element, headerDepth = 6){
const headerSelector = Array(headerDepth).fill().map((i,idx) => `h${idx+1}`).join(',');
console.log(headerSelector);
if (element.matches(headerSelector)) {
return element;
}
const tempAttr = 'data-getSectionInTitleElement';
element.setAttribute(tempAttr, "");
const els = document.querySelectorAll(`${headerSelector},[${tempAttr}]`);