Jump to content

Displaying array with innerHTML


phppup

Recommended Posts

My code is a mess from various attempts at a simple task (it is embarrassing). And also frustrating (especially since my searching hasn't found a solution, or a more important explanation of WHY a method works).

I want to have my stay contents displayed in a specific HTML container.

 

fruits = ["apple", "orange", "cherry"];

x = document.getElementById("myList").innerHTML;

for (x = 0; x< fruits. length; x++){
  
document.getElementById("demo").innerHTML = x;
  
  }

I need a solution. And would appreciate comments for educational understanding.

 

Thanks.

Link to comment
Share on other sites

Here's what your code is attempting to do. Does it look anything like what you intended?

    fruits = ["apple", "orange", "cherry"];               // define an array of fruits

    x = document.getElementById("myList").innerHTML;      // get text content from mylist and put in x 
                                                          // (I have no idea how this relates to rest of the code)

    for (x = 0; x< fruits.length; x++) {                  // now use x as a counter to loop throught fruits array
                                                          // (whatever you put in x above is now replaced by the counter values)
                                                          
                                                          
        document.getElementById("demo").innerHTML = x     // each time through the loop put the value of x (0,1,2) into demo
                                                          // so, at the end of the loop, demo should have value of 2.
    }

 

Link to comment
Share on other sites

Copied a wrong line.

But still...

fruits = ["apple", "orange", "cherry"];

for (x = 0; x< fruits.length; x++){
  
document.getElementById("demo").innerHTML = fruits[x];
  
  }

What would get this working?

Would for each be any better?

 

Edited by phppup
Link to comment
Share on other sites

What is HTML markup for that page? Then we can get an idea of what you are trying to do.

Do you realize you can only use an id once - they have to be unique?

The first time throught the loop will put apple into demo. The second time will overwrite apple with orange. Finallly you will finish with just cherry in the demo element.

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.