<span>HELLO WORLD <a>fwefw</a> iwerghefiwuehfiuweh</span>
JavaScript
const _store = {
referencedEl: {},
templates: {},
singleEvents: new WeakMap(),
singleEventsAncestors: []
}
// If traversing from `document` is a problem you should be using a
// virtual-list to manage off-screen DOM elements
const query = selector => document.querySelector(selector)
const queryAll = selector => [...document.querySelectorAll(selector)]
// Removes all child nodes not just children
const replaceChildNodes = parent => {
while (parent.hasChildNodes()) {
parent.removeChild(parent.lastChild)
}
}
// Gets a list of all ancestors up to the body
const getAncestors = (el) => {
const ancestors = [el],
{
body
} = document
while (el.parentElement !== body) {
el = el.parentElement;
ancestors.push(el)
}
return ancestors
}
const storeReferencedEl = (selector, HTML, ref) => {
if (!_store.templates[selector]) {
console.error(`Template ${selector} is in use.`)
return
}
// Add the new template
const template = _store.templates[selector] = document.createElement('template')
// Add a base element to template
template.append(document.createElement('div'))
const {
firstElementChild
} = template
// Create the HTMl and insert the into the base element
firstElementChild.insertAdjacentHTML('afterbegin', HTML)
// Store the referenced element
_store.referencedEl[ref] = firstElementChild.firstElementChild
}
const removeDescendentEvents = (el) => {
const eventsToRemove = _store.singleEventsAncestors.reduce((acc, entry, i) => {
if (entry.indexOf(el) > -1) {
_store.singleEventsAncestors[i] = null
acc.push(entry[0])
}
return acc
}, [])
// Remove events
eventsToRemove.forEach(boundedElement => {
const elEvent = _store.singleEvents.get(boundedElement)
const {
selector,
event,
eventHandler,
options
} = elEvent
boundedElement.removeEventListener(event, eventHandler, options)
boundedElement = null
})
}
const...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.