How to concatenate variable in element id? jQuery

1

Hello, I'm trying to return information from a .php file in a specific div using jQuery. The situation is as follows: The information will be retuned to a div with id="bkpLoja", but this div is in a section with query in the database and each registered store has its div in the following pattern "bkpLoja". $ Id_da_loja ; Follow the example

<div id=bkpLoja1>
conteudo...
</div>

<div id=bkpLoja2>
conteudo...
</div>

<div id=bkpLoja3>
conteudo...
</div>

the jQuery code looks like this

   function bkpLojas(id,bd) {
   var resposta = confirm("Deseja realmente realizar backup deste registro?");

   if (resposta == true) {
        $.ajax({
          type:'GET',
          url:'exemplo.com.br/up.php',
          data:'id='+id+"&bd="+bd,
          success:function(data){
            $("#bkpLoja").attr(id).html(data);
          },
          error:function(data){
            alert("Ops! Erro:05, entre em contato com o suporte!");
          }
        });
       }
     }

I've tried it so tb:

$("#bkpLoja"+id).html(data);

From now on I thank you for your attention!

    
asked by anonymous 12.04.2017 / 14:43

1 answer

2

Of the two forms you used, only the second form:

$("#bkpLoja"+id).html(data);

... It will fill the div correctly.

The problem is not your jQuery code. The problem is the data object. I suggest you check it in the browser console (in most browsers, it can be accessed by pressing F12). See the value of the object data . Depending on how formatted it may be that its value results in something invisible in your div's.

Editing:

I believe that the div's should be declared as follows:

<div id="bkpLoja1">
    conteudo...
</div>

<div id="bkpLoja2">
    conteudo...
</div>

<div id="bkpLoja3">
    conteudo...
</div>

Note that quotation marks make the difference. Without quotation marks the id attribute is not valid, then it is also possible for the browser to render the div's without the attribute and so jQuery does not find them.

    
12.04.2017 / 14:52