How to update the screen as filter inserted as pure Javascript in the search?

Asked

Viewed 58 times

2

inserir a descrição da imagem aqui

The data is arranged this way, but I want in the search field to insert an 'a', then only the fields containing letter 'a' appear. But if I put 'as' right away, the search should be updated. but it is cumulative. How to update?

    let data = null;

request.onload = function() {
    data = JSON.parse(this.response);
}

var filtro = document.getElementById('filtro-pesquisa');

filtro.onkeyup = function() {
    var nomeFiltro = filtro.value;

    data.results.forEach(x => {      
        if((x.name.first.toLowerCase().indexOf(nomeFiltro) >= 0) || (x.name.last.toLowerCase().indexOf(nomeFiltro) >= 0)){
            buildCard(x.name, x.gender, x.picture, x.registered);
        }
    });
};

1 answer

3


I did, just by inserting a line to remove the content generated by the previous filter:

filtro.onkeyup = function() {
    var nomeFiltro = filtro.value;
    document.getElementById('itemContainer').innerHTML = ""; // linha inserida
    // etc...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.