forked from marquez.juan/clase-10-ejercicios-de-repaso
ejercicio 5: filtro de lista en tiempo real
This commit is contained in:
@@ -1 +1,43 @@
|
||||
// Agregar aquí el código javascript
|
||||
const paises = [
|
||||
'Argentina',
|
||||
'Brasil',
|
||||
'Canadá',
|
||||
'Dinamarca',
|
||||
'España',
|
||||
'Filipinas',
|
||||
'Grecia',
|
||||
'Hungría',
|
||||
'India',
|
||||
'Japón',
|
||||
'México',
|
||||
'Noruega'
|
||||
];
|
||||
|
||||
const filtroInput = document.getElementById('filtro');
|
||||
const listaPaises = document.getElementById('lista-paises');
|
||||
|
||||
function renderizarLista(items) {
|
||||
listaPaises.innerHTML = '';
|
||||
|
||||
items.forEach((pais) => {
|
||||
const li = document.createElement('li');
|
||||
li.textContent = pais;
|
||||
listaPaises.appendChild(li);
|
||||
});
|
||||
}
|
||||
|
||||
function filtrarPaises(texto) {
|
||||
const valor = texto.trim().toLowerCase();
|
||||
if (valor === '') {
|
||||
return paises;
|
||||
}
|
||||
|
||||
return paises.filter((pais) => pais.toLowerCase().includes(valor));
|
||||
}
|
||||
|
||||
filtroInput.addEventListener('input', (event) => {
|
||||
const paisesFiltrados = filtrarPaises(event.target.value);
|
||||
renderizarLista(paisesFiltrados);
|
||||
});
|
||||
|
||||
renderizarLista(paises);
|
||||
|
||||
Reference in New Issue
Block a user