4
To display only the element in which the searched word is found?
In the example below he searches us filhos
of container
if the searched content is present in the element moves it to the saida
.
But in that case by the "parent" element having the word searched, it will be moved whole to the saida
, and not the element in which the word is found.
function pesquisar(){
let container = document.getElementById("container");
let saida = document.getElementById("saida");
let input = document.getElementById("input").value.toLowerCase();
let filhos = container.children;
for(let i = 0; i < filhos.length; i++){
if(filhos[i].innerText.toLowerCase().includes(input) || filhos[i].innerText.toLowerCase().indexOf(input) > -1){
saida.appendChild(filhos[i]);
}
}
}
<input type="text" id="input"><button onclick="pesquisar()">Pesquisar</button>
<h3>Entrada</h3>
<div id="container">
<div class="pai">
<div class="pai">
<div class="filho">Conteudo</div>
<div class="filho">Pesquisado</div>
</div>
</div>
</div>
<h3>Saida</h3>
<div id="saida"></div>