phppup Posted October 13, 2021 Share Posted October 13, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313974-displaying-array-with-innerhtml/ Share on other sites More sharing options...
Barand Posted October 13, 2021 Share Posted October 13, 2021 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. } Quote Link to comment https://forums.phpfreaks.com/topic/313974-displaying-array-with-innerhtml/#findComment-1590984 Share on other sites More sharing options...
phppup Posted October 13, 2021 Author Share Posted October 13, 2021 (edited) 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 October 13, 2021 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/313974-displaying-array-with-innerhtml/#findComment-1590987 Share on other sites More sharing options...
Barand Posted October 13, 2021 Share Posted October 13, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313974-displaying-array-with-innerhtml/#findComment-1590991 Share on other sites More sharing options...
phppup Posted October 13, 2021 Author Share Posted October 13, 2021 I think I'm on the right track now. Remembered how to LIST to avoid the overwrite. Also, my SIMPLE div with its unique id was AFTER the script (which apparently prevented ANY result from appearing in the area). Quote Link to comment https://forums.phpfreaks.com/topic/313974-displaying-array-with-innerhtml/#findComment-1590993 Share on other sites More sharing options...
Barand Posted October 13, 2021 Share Posted October 13, 2021 You almost had it fruits = ["apple", "orange", "cherry"]; for (x = 0; x< fruits.length; x++) { document.getElementById("demo2").innerHTML += fruits[x] + "<br>"; } or fruits = ["apple", "orange", "cherry"]; $.each(fruits, function(k,v) { $("#demo").append(v + "<br>") }) Quote Link to comment https://forums.phpfreaks.com/topic/313974-displaying-array-with-innerhtml/#findComment-1591000 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.