Skip to content

Instantly share code, notes, and snippets.

@dmertl
Last active May 16, 2024 21:01
Show Gist options
  • Save dmertl/0aaf0a5fd07dfc2f23d99c681a15b878 to your computer and use it in GitHub Desktop.
Save dmertl/0aaf0a5fd07dfc2f23d99c681a15b878 to your computer and use it in GitHub Desktop.
Javascript Cheatsheet

Unique ID

// Generates a unique (enough) ID from current time in milliseconds + random
function uid() {
    return Date.now().toString(36) + Math.random().toString(36).substr(2, 12);
}

Watch for Elements Added to DOM

const list = document.getElementById('theList');
const obs = new MutationObserver(function(mutations, observer) {
    for (const mutation of mutations) {
        for (const node of mutation.addedNodes) {
            decorateListItem(node);
        }
    }
});
obs.observe(list, {"childList": true});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment