Here is a working codepen: https://codepen.io/adebolle/pen/eYdbbad
HTML:
This is my list:
<ul id = "subCategoryUL" onclick="selectSubCategory()">
</ul>
CSS:
#subCategoryUL {
min-width: 50px;
min-height: 50px;
background-color: red;
}
JS
var names = ["Value1", "Value2", "Value3"];
function selectSubCategory(){
var selectedSubCategory = document.getElementById('subCategoryUL');
let index = selectedSubCategory.getElementsByTagName('li').length % names.length;
var li = document.createElement('li');
li.innerHTML = names[index];
li.style.font.fontsize(150);
document.getElementById('subCategoryUL').appendChild(li);
console.log("subCategoryULR "+selectedSubCategory.innerText);//+subCategory);
}
I added CSS styling, because without it your empty list will have near to 0 height, adding the color makes sure we know where to click. For the javascript function I added a modulus operation to “loop” over the names.