How to return to the "normal state" of Html after a refresh-free Javascript query?

Asked

Viewed 396 times

0

all right? I’m having a very boring question here and I haven’t been able to find any viable solutions. Well, I’ll explain the problem.

This is my class where I will return my query

<div class="col-lg-2 result"> 
    ...  AQUI DENTRO ESTARÁ A CONSULTA INICIAL DA PAGINA
</div>

and this is my code to make every letter I type return the query to me

$(function(){
//Pesquisar os cursos sem refresh na página
$("#pesquisa").keyup(function(){

    var pesquisa = $(this).val();

    //Verificar se há algo digitado
    if(pesquisa != ''){
        var dados = {
            palavra : pesquisa
        }       
        $.post('busca.php', dados, function(retorna){
            $(".result").html(retorna);
        });
    }else{
        $('.result').load('');
    }       
});
});

Now the problem, when I do the search she subscribes to div ( this I want to happen), but when deleting all typed letters, I need the div back to the original state, ie the state in which only my php query acts on the code.

Someone would have a tip on how to fix this?

  • You have tried using Jquery Autocomplete ?

  • Why php does not return this html in case the search is empty?

  • https://answall.com/questions/210711/combobox-ajax-php-nao-retorna-nada?noredirect=1#comment430860_210711

1 answer

1


Just you save the data from div.result before the first interaction. See the variable htmlOrig. When you have nothing else to show, go back to the previous place and your HTML goes back to its original state.

$(function(){

var htmlOrig = $('.result').html();

//Pesquisar os cursos sem refresh na página
$("#pesquisa").keyup(function(){

    var pesquisa = $(this).val();

    //Verificar se há algo digitado
    if(pesquisa != ''){
        var dados = {
            palavra : pesquisa
        }       
        $.post('busca.php', dados, function(retorna){
            $(".result").html(retorna);
        });
    }else{
        $('.result').html(htmlOrig);
    }       
});
});

  • Okay Ricardo, I will study this method. And tell me, this is a good solution/practice to solve this problem?

  • is a solution. If you solve your case there depends on what is inside the div.result, mainly.

  • Okay, I’ll see if you’ve solved it, in case you haven’t solved it I’ll come back here with more details

Browser other questions tagged

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