3
Good night,
I need to sort a list of HTML phrases by user name or age, depending on which button is clicked. How can I do this?
Below is a clearer version of the code:
<html>
<head>
<title>JS</title>
</head>
<body>
<div id="master">
<button onClick="orderName()">Ordenar por nome</button>
<button onClick="orderIdade()">Ordenar por idade</button>
<br/>
<ul id="lista-usuarios">
<li>João é homem e possui 23 anos"</li>
<li>Ana é mulher e possui 19 anos"</li>
<li>Ricardo é homem e possui 32 anos"</li>
<li>Fernanda é mulher e possui 25 anos"</li>
</ul>
</div>
<script>
function orderName() {
var list = document.querySelector("#lista-usuarios");
console.log("Ordenado por nome");
}
function orderIdade() {
var list = document.querySelector("#lista-usuarios");
console.log("Ordenado por idade");
}
</script>
</body>
</html>
UPDATING:
If necessary, this is the full version.
Phrases are mounted from the array users
(as seen in the link above). I was able to make the filters work, so I chose to reduce the code, to facilitate.
Like I need the ordering to work together with the filters, I understood that it might be necessary to take the list from the HTML
, to keep the filters in order (in my attempts, if I took from the array, I would have to clear the list, which would eliminate the filter).
Where does this list come from?
– novic
I changed the question explaining better how the list is obtained, @Virgilionovic
– EdeiltonSO