Skip to content

Instantly share code, notes, and snippets.

View aminimalanimal's full-sized avatar

James Dawson aminimalanimal

View GitHub Profile
@aminimalanimal
aminimalanimal / Debugging: Focus: on focus and blur, log document.activeElement.js
Last active October 21, 2022 17:35
Debugging: Focus: on focus and blur, log document.activeElement
/* Non-jQuery, ES6 version. This won't definitely work in older browsers */
/* Short ES6 version (for single-line copy/pasting) */
const everything = document.querySelectorAll('*'); function trackEvent(event) { event.stopPropagation(); var eventType = event.type, eventTarget = event.target, activeElement = document.activeElement; setTimeout(function() { console.group(eventType); console.log('event.target:'); console.log(eventTarget); console.log('document.activeElement (immediate):'); console.log(activeElement); console.log('document.activeElement (after cycle completion):'); console.log(document.activeElement); console.groupEnd(); }, 0); }; ['click', 'focus', 'blur'].forEach((eventType) => { everything.forEach((el) => el.addEventListener(eventType, trackEvent));});
/* Expanded ES6 version (for editing/understanding) */
const everything = document.querySelectorAll('*');
function trackEvent(event) {