Skip to content
tinytip

Latest tips / 14

Insert multiple HTML elements at once with DocumentFragment

June 19, 2021
#javascript

When you need to create multiple HTML elements and insert them into the DOM, you can use a document fragment. A document fragment allows you to assemble all elements and insert them all at once. That is more efficient than adding them one by one.

// Create a new fragment
const fragment = document.createDocumentFragment();

const item1 = document.createElement("div");
item1.innerText = "item 1";
fragment.appendChild(item1); // Append a child

const item2 = document.createElement("div");
item2.innerText = "item 2";
fragment.appendChild(item2); // Append another child

// Append all child to the document
// The document fragment will not be part of the DOM
document.body.appendChild(fragment);

// The document fragment is now empty
console.log(fragment.childElementCount); // logs 0

Find files by name on GitHub

June 17, 2021
#github

Open a repository on GitHub and press t to open the File Finder. This let's you find files by name (or name of the directory) in the current repo.

File Finder on GitHub

Emulate a focused page in DevTools to debug overlay elements

June 17, 2021
#devtools

Some overlay elements like dropdowns or flyout menus automatically hide/remove themselves when the focus is lost. Whenever you switch to the DevTools the page is not focused anymore and the element gets hidden.

The Chrome DevTools have a feature to emulate page focus. Open the DevTools, press Ctrl+Shift+P (or Cmd+Shift+P on macOS) and search for "Emulate a focused page".

Emulate a focused page in DevTools

Navigate easier in large files via symbols in VS Code

June 14, 2021
#vscode

When you open a file in VS Code and you look for a specific function, class, ... you can navigate by symbols. Press Ctrl+Shift+O (or Cmd+Shift+O on macOS) to see all symbols of the current file. You can filter the list and navigate through the results with arrow down / arrow up. VS Code will automatically scroll to the focused element. Press esc to close the list (and navigate back to the original scroll position) or enter to focus that symbol.

This works for many different languages like JavaScript, TypeScript and even headings in Markdown.

Symbol Search in VS Code