I need to display numbers which each list item in an un-ordered list. To achieve this, I tried to convert an un-ordered ul list tag into an ordered ol list tag (see my script below). I need to do this because I want each li tag to display its order number next to it.
Here is the script that I tried which I found on codepen:
var ul = document.getElementsByClassName("productGrid")[0]
var ol = document.createElement("ol");
var li = ul.querySelectorAll('li');
for(var i=0;i<li.length;i++) {
ol.appendChild(li[i]);
}
setTimeout(function() {
ul.parentNode.replaceChild(ol,ul);
},1000);
This works but as expected it ruined the "styles" set to that ul tag. So instead of turning it into an actual ol tag, I need to keep it as a ul and display numbers some other way.
How can I use JavaScript to find out how many li's are inside the ul tag and then display the appropriate number within the li tag (via something like a span tag)
Any help with this would be much appreciated.
ol1is not a tag name I've ever heard of? Did you meanol? Ifol, that will select an existing ordered list, not an existing unordered listlist-style: decimal?