Is it possible to sort list items by numbers within a strong tag using JavaScript code? The current code successfully sorts the numbers, but removes them from the div tag. (The JavaScript code used below is for sorting by Name and works properly when 'b = ...(LI)'.) HTML:
<section>
<button class="sortbynum" onclick="sortListNum()">Sort By Num</button>
<ul id="sort">
<li>
<div><h2>Name</h2></div>
<a href=""><img src=""/></a>
<div><span>Something:...</span></div>
<div><span>something:...</span></div>
<div><span>HHJS: <strong class="sortNum">456</strong></span></div>
</li>
<li>again</li>
</ul>
JavaScript:
function sortListNum() {var list, i, switching, b, shouldSwitch, dir,switchcount = 0;list = document.getElementById("sort");switching = true;dir = "asc"; while (switching) {switching = false;b = list.getElementsByClassName("sortNum");
for (i = 0; i < (b.length - 1); i++) {
shouldSwitch = false;
if (dir == "asc") {
if (b[i].innerHTML.toLowerCase() > b[i + 1].innerHTML.toLowerCase()) {
shouldSwitch= true;
break;
}
} else if (dir == "desc") {
if (b[i].innerHTML.toLowerCase() < b[i + 1].innerHTML.toLowerCase()) {
shouldSwitch= true;
break;
}
}
}
if (shouldSwitch) {
b[i].parentNode.insertBefore(b[i + 1], b[i]);
switching = true;
switchcount ++;
} else {
if (switchcount == 0 && dir == "asc") {
dir = "desc";
switching = true;
}
}}}