Tools logo

Useful Code Archive

This is more for me than anyone else, but if you're helped by this then I see that as an absolute win.


JSON array to HTML elements in a grid

//define variables const arrayHolder = document.getElementById('arrayHolder') let array = [ { title:'wasd', text:'the first item' }, { title:'ijkl', text:'the second item' }, { title:'tfgh', text:'the third item' } ] function addItems(amount, sort) { //run if sort goes up or down if (sort == 'down') { for (let i = 0; i < amount; i++) { addItemToHolder(i) } } if (sort == 'up') { for (let i = 0; i < amount; i++) { addItemToHolder(amount-i) } } //run if sort is an array if (sort != 'down' && sort != 'up' && sort[0] != undefined) { for (let i = 0; i < sort.length; i++) { addItemToHolder(sort[i]) } } } function addItemToHolder(i) { //create html array item item = document.createElement('div') item.innerHTML = ` <h2>`+array[i].title+`</h2> <p>`+array[i].text+`</p> ` arrayHolder.appendChild(item) }


Copy text

navigator.clipboard.writeText('text')