In the xmlhttp.onreadystatechange function, how do I pass the name of the identifier I want to change?

Here is my code. See the comment line. When an element identifier (which is a range) is hard-coded, it works. When an id is created by concatenating the variables passed to stateChanged, this does not work. Am I not allowed to pass variables to stateChanged? What's wrong?

function multiplePassportPoints(id, counter)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="addmorepoints.php"; 
url=url+"?id="+id+"&c="+counter;
url=url+"&sid="+Math.random();

xmlhttp.onreadystatechange=stateChanged(id,counter);
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged(id, counter) 
{
  if (xmlhttp.readyState==4)
  {
    //THIS WORKS (assuming id is 99 and counter is 5:
    //document.getElementById("99_5").innerHTML += xmlhttp.responseText; 

    //BUT I NEED IT TO WORK LIKE THIS:
    document.getElementById(studentID+"_"+counter).innerHTML += xmlhttp.responseText; 
  }
}

Thanks!

+3
source share
3 answers

You can change the code to this

xmlhttp.onreadystatechange = function () {
        stateChanged(id,counter);
    };    
+8
source
 <script type="text/javascript">
 var i=1;
 function validate(str)
 { 
 xmlHttp=GetXmlHttpObject()
 var url="checkvalidate.php"
 url=url+"?User9="+str
 xmlHttp.onreadystatechange=stateChanged19 
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)
 }
function stateChanged19() 
 {  

 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  { 

  document.getElementById("valid"+i)
  .innerHTML=xmlHttp.responseText 
  i=i+1;
   } }
 </script>
+1
source

, . , multiplePassportPoints xmlhttp, . :
1- , concurrency ( ),
2- ( , )

( ) .

function multiplePassportPoints(id, counter) {
  var xmlhttp=GetXmlHttpObject();
  if (xmlhttp==null)
  {
    alert ("Browser does not support HTTP Request");
    return;
  }

  var url="addmorepoints.php"; 
  url=url+"?id="+id+"&c="+counter;
  url=url+"&sid="+Math.random();

  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
      stateChanged(id, data, xmlhttp.responseText);
    }
  };

  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}

function stateChanged(id, counter,text) 
{
    document.getElementById(id+"_"+counter).innerHTML += text; 
}
0

Source: https://habr.com/ru/post/1717706/


All Articles