Question

Working with Ajax... I cannot seem to figure out what is wrong here. The error occurs on the code: objUserID.innerHTML = username;. It thinks the variable username is null. username does have data in it because the following code confirms it: 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;
      }
   }
}
Était-ce utile?

La solution

It thinks the variable username is null

False. It is telling you that it cannot access the property innerHTML of null. In other words, that objUserID is null and that you cannot access a property of it.

Put another way, your element does not exist.

Autres conseils

If you are having this problem, it might be because you have placed your script tag at the top of the body tag before everything else. You want to place your script tag at the bottom of the body tag.

Actually it was an loading issue check with the follow code.

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

It means that the element or the object is not found. It doesn't exist.

Here is a fiddle: http://jsfiddle.net/afzaal_ahmad_zeeshan/cF6Bh/

You can see, that the code works. But the element's not present for the JavaScript to work on.

Make sure that the element is present. Either you need to make sure the characters are OK or something like that.

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

So the remedy to this would be, to change the ID param that you're passing onto the method.

actually this error can be also caused by calling like this document.getElementById("#content-content").innerHTML=output;

instead of like this

document.getElementById("content-content").innerHTML=output;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top