Exchange values from a div

Asked

Viewed 153 times

0

I have a combo when the event occurs onchange it performs a query in the database with the selected value and inserts it into a div values, the problem is that when I apply $('.minhaDiv').html(resultado) it returns me only the last data, I tried with append and it repeats several data below the previously consulted ones, what I would need would be that the same superimposed with all data the values previously arranged on the screen.

How could I do this? Any solution?

an example of my code:

$("#dados_solicitacao").on('change','#caminho', function change(){
   var selected = $(this).val();
   var url = urlprojeto+"/minhaUrl.php";
            var data = 'referencia='+selected+'';
            var data = get_contents(url, data);
            var aDados  = data.dados;
            var cDados  = aDados.length;
            for( s=0; s<cDados; s++ )
                {

                    var status      = aDados[s].cad_ativo;
                    var referencia  = aDados[s].cad_referencia;
                    var txt= 'ativo:'+status;
                    txt+= ' minha referencia:'+referencia;
                    $(".div_selected").html(txt);
                }
});

O que tenho na tela, e gostaria que fosse sempre trocado ao mudar o valor selecionado no combo

What I have on the screen, and I would like it to be always changed when changing the value selected in the combo.

Because they are different data it is necessary to replace them, because my combo serves as a filter, where in this combo I navigate through my "folders" (topics of my site).

  • the correct would not be you put $(".div_selected").html(txt); outside the block of for?

  • I’ve done this and keep returning only the last interaction of for

  • 2

    If possible post as is your date. What is printing and what should print. It will help a lot.

  • The way you are doing, the contents of the div will be replaced every time you enter the loop. The function html overwrite all html by new. You have to use append or prepend to do this, as for the problem you reported, I think it’s with your data.

  • but how could I do it any other way, because I know that html changes values, and inside it will change every time it goes through the loop

  • 1

    Marcos, can you explain HTML better and what you want to do? you have a select and how many elements should get what’s in txt? You’re re-declaring the variables inside the for why: it must have different values and not concatenate?

Show 1 more comment

1 answer

1


Try this,

$("#dados_solicitacao").on('change','#caminho', function change(){
   var selected = $(this).val();
   var url = urlprojeto+"/minhaUrl.php";
            var data = 'referencia='+selected+'';
            var data = get_contents(url, data);
            var aDados  = data.dados;
            var cDados  = aDados.length;
            var txt = '';
            for( s=0; s<cDados; s++ )
                {

                    var status      = aDados[s].cad_ativo;
                    var referencia  = aDados[s].cad_referencia;
                    txt+= 'ativo:'+status;
                    txt+= ' minha referencia:'+referencia;

                }
              $(".div_selected").html(txt);
});

See that you were declaring the txt variable inside the for, now the txt value will suffer an append.

See if that solves your problem.

  • I appreciate the help, I’d forgotten this detail.

  • 1

    In need of help, give a shout!! Hugs

  • @Rboschini, you know this function change() {...} could be anonymous: function() {...}, because the person who deals with change is the .on('change', ...)

  • this function is an eventListener, it listens to the control change event, you can assign it to a variable and use it in more than 1 place, but make anonymous the way you asked will give error.

Browser other questions tagged

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