0
I need to make a filter using pure Javascript.
But by doing the filter and sorting by Name (a, b), for example, I can only sort the text elements out of context and not the complete block.
For example:
I need that block <div class="item">
be ordered along with <span>
:
<div id="produto">
<div class="item">
<span class="titulo">C</span>
</div>
<div class="item">
<span class="titulo">B</span>
</div>
<div class="item">
<span class="titulo">A</span>
</div>
The code I have is this:
window.onload=function(){
var div = document.querySelector('#produto'),
para = document.querySelectorAll('.item span.titulo');
var paraArr = [].slice.call(para).sort(function (a, b) {
return a.textContent > b.textContent ? 1 : -1;
});
paraArr.forEach(function (p) {
div.appendChild(p);
});
}
.item{
display: block;
border: 1px solid #DFDFDF;
padding: 8px;
margin: 8px;
}
<!DOCTYPE html>
<html>
<body>
<div id="produto">
<div class="item">
<span class="titulo">C</span>
</div>
<div class="item">
<span class="titulo">B</span>
</div>
<div class="item">
<span class="titulo">A</span>
</div>
</div>
</body>
</html>
Thanks :D really create the elements ends up becoming easier
– Lidiane Alencar