How to use a variable in getElementById (AJAX & PHP) [closed]

0

The code is a function in AJAX that receives from a PHP page 2 variables: id and nombrediv . nombrediv is the id of the div where I want to show the result of the ajax function, but it does not work.

Can this be done?:

document.getElementById(VARIABLE)

What is the way to write it?

var editarMedidas = function(id, nombrediv) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "editar-medidas.php?id=" + id + "&nd=" + nombrediv);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if(xhr.status == 200) {
                // AQUI ES EL PROBLEMA, SE PUEDE PONER UNA VARIABLE PARA QUE BUSQUE LA DIV CON ESE NOMBRE?                  
                var caja=document.getElementById(nombrediv);     
                caja.innerHTML = xhr.responseText;     
            }        
        }                 
    }
    xhr.send(null); 
}
    
asked by Juan 14.08.2017 в 10:50
source

2 answers

1

I recommend using ajax for that kind of operations together with jquery, life will be much easier for you. In my modest opinion compare: var div = document.getElementById ("div1") // Default var div = $ ('# div1') // using jquery

    
answered by 14.08.2017 в 16:17
0

If of course it's possible! I'll give you an example

var aux = "contenedor";

var contenedor = document.getElementById(aux);

contenedor.innerHTML = "Estoy dentro del div" ;
<div id="contenedor">
  
</div>
    
answered by 14.08.2017 в 11:48