Jump to content

Displaying multiple results with array


jnjk

Recommended Posts

I am having problems populating multiple divs with different content. I have an array that contains a url with two different parameters. The html contains two divs with the id's updateArea0 and updateArea1. The result I get is that only updateArea1 is populated with the results of the second array element. Here is the code.

[code]window.onload = initAll;

var xhr = null;
var categories = new Array("ajax.php?cats=engines", "ajax.php?cats=computers");

function initAll() {
        for(var i=0; i<2; i++) {
                getNewFile(categories[i],i);
        }

}

function getNewFile(url,i) {
        makeRequest(url,1);
        return false;
}

function makeRequest(url,i) {
        if(window.XMLHttpRequest) {
                xhr = new XMLHttpRequest();
        } else {
                if(window.ActiveXObject) {
                        try {
                                xhr = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch(e) {}
                }
        }

        if(xhr) {
                xhr.onreadystatechange = function() { showContents(i) };
                xhr.open("GET",url,true);
                xhr.send(null);
        }
        else {
                document.getElementById("updateArea").innerHTML = "Sorry but the data is unavailable";
        }
}

function showContents(i) {
        x = i + 7;
        currDiv = "updateArea" + i;
        if(xhr.readyState == 4) {
                if(xhr.status == 200) {
                        var outMsg = (xhr.responseXML && xhr.responseXML.contentType == "text/xml") ?

                xhr.responseXML.getElementsById("choices")[0].textContent : xhr.responseText;
                }
                else {
                        var outMsg = "There was a problem with the request " + xhr.status;
                }
               
                elem = document.getElementById(currDiv);
                elem.innerHTML = outMsg;
               

        }
}
[/code]

Thanks in advance.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.