Faculty of Engineering and Mathematical Sciences 
Not logged in (login)

help3403


This forum is provided to promote discussion amongst students enrolled in Agile Web Development (CITS3403).
 
Options:
RSS cloud
Jump to:

question about show the things in array in html pages

2 of 419 articles shown, currently no other people reading this forum.
photo
From: ANONYMOUS
Date: Sat 28th Mar 2020, 7:02pm
Actions: 
        Login-to-reply

 

Assume there is an array which name is "check"

var check=[apple,pear,orange,banana,peach]

As we know that the way to print the array out inside of the html page is:
document.getElementById("demo").innerHTML=check;

the result comes out is apple,pear,orange,banana, peach  and the result is inside 
of the html, but how can we print it separately? I mean how to do like in java, 
we use a for loop and println(check[num]) then we can get things like below.

apple
pear
orange
banana
peach


i tried for loop with document.getElementById("demo").innerHTML=check; , the 
result still be apple,pear,orange,banana, peach

then i tried for loop with document.getElementById("demo").innerHTML=check[num] , 
the result only get the final element in the array, get the "peach".

by the way, i found that document.getElementById("demo").innerHTML=check[] will 
be update by the newest result, like if the first result is "apple", then reuse 
this function, the result will be "pear" and the "apple" will disappear.

looking for help, thanks!

question about show the things in array in html pages

photo
From: Haolin W.
Date: Sat 28th Mar 2020, 9:29pm
Actions: 
        Login-to-reply

 

Hey. It's hard to interpret what exactly you need, but based on your code, I'm 
assuming that you're trying to replace the content of one of your elements with this 
array. Assuming that the element that you're placing the fruits into is empty, you can 
loop over your check array and create fruit elements, and then append them as children 
of the container element (see Lab 4 appendChild).

I've written a quick codepen as an example so you can see what's going on here. Keep 
in mind, this assumes that the container element (in your case, 'demo') is empty.

https://codepen.io/haolinwu/pen/gOpZKbY

There are many ways to tackle this problem, and this solution is only one. Depending 
on your scenario, you could do this much differently.

Let me know how you go.
This Page


Program written by: [email protected]
Feedback welcome
Last modified: 11:27am Sep 21 2020