Uncaught TypeError: Unable to set 'innerHTML' property from null

Working with Ajax ... I cannot understand what is wrong here. The error occurs in the code: objUserID.innerHTML = username; . He believes the username variable is NULL. the username has data in it, because the following code confirms this: console.log ("user: [" + username + "]"); Can anyone figure this out?

function actionBid(bidID,bidA,bidAction){
   var XMLHttpRequestObject = false;

   if (window.XMLHttpRequest)
   {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      XMLHttpRequestObject = new XMLHttpRequest();
   }
   else if (window.ActiveXObject) 
   {
      // code for IE6, IE5
      XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
   }
   if(XMLHttpRequestObject)
   { 
      // ==== GET BID ====
      if (bidAction == "getbid"){

      var objUserID = document.getElementById("curBidUser"+bidID); 
      var res = XMLHttpRequestObject.responseText;
      var username = res.substring(0,res.indexOf(','));
      console.log("user: ["+username+"]");
      objUserID.innerHTML = username;
      }
   }
}
+4
source share
5 answers

He thinks the username variable is null

False. , innerHTML null. , objUserID null .

, .

+9

, , , script . script body.

+6

, . .

: http://jsfiddle.net/afzaal_ahmad_zeeshan/cF6Bh/

, . , JavaScript .

, . , , - .

document.getElementById("objectId").innerHTML = "Text";

, ID, .

0
source

Actually it was a boot validation issue using the following code.

setTimeout(function(){ 
  xYzFunction();    
}, 3000 )
0
source

in fact, this error can also be caused by a call document.getElementById("#content-content").innerHTML=output;

instead of this

document.getElementById("content-content").innerHTML=output;
0
source

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


All Articles