Skip to content

Instantly share code, notes, and snippets.

@odoe
Created November 6, 2016 19:22
Show Gist options
  • Save odoe/147532e719b5860f867d81c75267f113 to your computer and use it in GitHub Desktop.
Save odoe/147532e719b5860f867d81c75267f113 to your computer and use it in GitHub Desktop.
Ghetto JSAPI layerlist toggle in JavaScript
function layerList(layers) {
const list = layers.reduce((ul, layer) => {
const li = document.createElement('li');
const toggle = document.createElement('input');
toggle.type = 'checkbox';
toggle.checked = layer.visible;
layer.watch('visible', () => toggle.checked = layer.visible);
toggle.addEventListener('change', () => layer.visible = !layer.visible);
const label = document.createElement('span');
label.innerHTML = layer.title;
li.appendChild(toggle);
li.appendChild(label);
ul.appendChild(li);
return ul;
}, document.createElement('ul'));
return list;
}
@steveoh
Copy link

steveoh commented Nov 7, 2016

yummy vanilla

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment