Jeff Thomas
Jeff Thomas

Reputation: 4816

Getting Type Error with getElementById

I am using the following code to make a call to another script:

function savecoupon<?php echo $r['id']; ?>() {
    var listingid = document.savecouponform<?php echo $r['id']; ?>.listingid.value;
    http.open('get', 'script/save_coupon.php?listingid='+listingid+'');
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
    if(http.readyState == 4){
        response = http.responseText;
        alert(response);
        document.getElementById(response).innerHTML = "Saved";
    }
}

The alert(response) is showing the proper results, which is the element id of the div that I am replacing. But for some reason, when I put the response into the getElementById I get the following error in firebug:

TypeError: document.getElementById(...) is null
document.getElementById(response).innerHTML = "Saved";

If I replace the innerHTML command with document.getElementById("savecouponarea1327").innerHTML = "Saved"; everything works as it is supposed to. How come the response is not being read in the getElementById?

Upvotes: 0

Views: 45

Answers (1)

epascarello
epascarello

Reputation: 207537

From basic debugging

console.log(escape(response));

showed that it has extra characters: %0D%0Asavecouponarea1327

%0D is the carriage return character. %0A is the linefeed character.

You have extra line in your server side code.

Upvotes: 1

Related Questions