<ul>
<li>text</li>
<li>text</li>
<li id="elem">text</li>
<li>text</li>
<li>text</li>
</ul>
const elem = document.querySelector('#elem').nextElementSibling;
if (elem) {
const num = 1 + [...elem.parentNode.children].indexOf(elem);
elem.innerText = num + '. ' + elem.innerText;
}
const elem = document.querySelector('#elem + *');
if (elem) {
let num = 1;
for (let n = elem; n = n.previousElementSibling; num++) ;
elem.textContent = `${num}. ${elem.textContent}`;
}
const elems = document.querySelector('ul').children;
const index = -~Array.prototype.findIndex.call(elems, n => n.id === 'elem');
if (index) {
elems[index]?.insertAdjacentText('afterbegin', ''.concat(-~index, '. '));
}