1

I'm studying javascript for the first time and I've some doubt: I have two arrays; the first one contain the image of a person, the second one his personal informations (the arrays' length is the same because they contain the same number of people). I'd like to write the content in my page so print the image and the infos of the first user, then the image and the infos of the second and so on...

How can I do it?

this is the code:

function jsonCard() 
{
var richiestaCard = new XMLHttpRequest();

richiestaCard.onreadystatechange = function()
{
    if(richiestaCard.readyState == 4) 
{
        var objectcardjson = {};
        window.arrayCard= []; //creazione dell'array che conterrà le cards
        objectcardjson = JSON.parse(richiestaCard.responseText);

        arrayCard = objectcardjson.cards; //the first array
    }
}
richiestaCard.open("GET", "longanocard.json", true);
richiestaCard.send(null);
}

function jsonEntity()
{
 var richiestaEntity = new XMLHttpRequest();

richiestaEntity.onreadystatechange = function()
{
    if(richiestaEntity.readyState == 4) 
{
        var objectentityjson = {};
        window.arrayEntity= []; //creazione dell'array che conterrà le entity
        objectentityjson = JSON.parse(richiestaEntity.responseText);

        arrayEntity = objectentityjson.cards; //the second array
    }
}
richiestaEntity.open("GET", "longano.json", true);
richiestaEntity.send(null);
}


function displayArrayCards()
{
jsonCard();
jsonEntity();  
var inizioTag = "<img src=";
var fineTag = "\>";

  for(i = 0; i < arrayCard.length; i++) 
 {
        ...
 }
}

I'd like to have the image and then the infos (for the first user), the image and the infos for the second and so on all included in a div.

2
  • Please show the code you have so far. Commented Jun 25, 2012 at 13:47
  • Any particular reason you are using div when this sounds exactly like what tables are for? Commented Jun 25, 2012 at 15:28

1 Answer 1

1

Try something along the lines of

 function displayArrayCards()
{
   jsonCard();
   jsonEntity();  

   var wrapper = document.getElementById('div you want to put all of these users into');
   for(i = 0; i < arrayCard.length; i++) 
   {
        var userDiv = document.createElement('div');

        var cardImg = document.createElement('img');
        img.src = arrayCard[i];
        /** Set other attributes for img **/

        var entityDiv = document.createElement('div');
        entityImg.innerHTML = arrayEntity[i];
        /** Set other attributes for div **/

        userDiv.appendChild(cardImg);
        userDiv.appendChild(entityDiv);

        wrapper.appendChild(userDiv);
   }
}

Honestly there are a lot of ways to do this. Above is simply what I would do.

Sign up to request clarification or add additional context in comments.

1 Comment

var cardImg = document.createElement('img'); cardImg.src = arrayCard[i]; I tried but it doesn't work in plus when I click the button again it doesn't clear the previous content. How can I fix it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.