Skip to content

Instantly share code, notes, and snippets.

@atomize
Created October 1, 2018 18:53
Show Gist options
  • Save atomize/dae428242ab3ba7eaaf891007dd9688b to your computer and use it in GitHub Desktop.
Save atomize/dae428242ab3ba7eaaf891007dd9688b to your computer and use it in GitHub Desktop.
Dynamically fill select options with a large list using document.createDocumentFragment() - much more efficient that document.createElement()
const fillMenu = (list, selector) => {
let subsSelect = document.getElementById(selector);
subsSelect.innerHTML = "";
let fragment = document.createDocumentFragment();
list.sort()
list.unshift("Select...")
list.forEach(function (element) {
let opt = document.createElement("option");
opt.value = element;
opt.innerHTML = element;
fragment.appendChild(opt);
});
subsSelect.appendChild(fragment)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment