How to concatenate variable in element id? jQuery

Asked

Viewed 1,615 times

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 resumed for a div with id="bkpLoja", however this div is in a query section in the database and each registered store has its div in the following pattern "bkpLoja". $id_da_store; Follow the example

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

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

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

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 that too:

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

Thanks in advance for the help attention of all!

  • The value that is coming in id that is correct?

  • yes, the script works normal (except the part I asked). It just doesn’t return to correct div what the . php sends as echo.

1 answer

2


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

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

... You will fill out the div correctly.

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

Editing:

I believe div’s should be stated as follows:

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

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

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

Note that quotation marks make a difference. No quotation marks id is not valid, so it is also possible for the browser to render div`s without the attribute and therefore jQuery cannot locate them.

  • before I was making $("#bkpLoja"). html(data); and returning the contents of . php correctly, but for div bkpLojas. I know how to open the console, but I don’t know how to see the date value.

  • On the console, write only data and press Enter.

  • @Jorgeborgonhibatista made an important issue. I believe it may be another cause of your problem.

  • Thanks @Renan was just that, in the file . php where the Divs were declared missing double quotes, lack of attention from me and thank you!!!

Browser other questions tagged

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