+ 2
Is there anyway to display items in an array without using document.write()
From what I have learnt so far, I know that document.write() is for testing purposes. So I was trying different methods but they were not working. Then I decided to consult my Sololearn friends if you can help??
3 Respostas
+ 1
You can select the element using document.querySelector("#example");
And then use innerHTML or innerText to set the content.
The difference between the two is that innerHTML sets the HTML markup and innerText only sets the text.
For example:
Assume in the HTML there is an item with an id of heroes
var heroes = document.querySelector("#heroes");
let namesOfHeroes = ["iron man", "spiderman", " superman"]
for(let i =0; i < namesOfHeroes.length; i++){
heroes.innerHTML += namesOfHeroes[i] + "<br/>"
}
+ 2
console.log();
and
alert();
are the another method to display arrays, booleans and so on.
Happy Coding!
0
Ryan Zico (Challenge Me) What methods did you try? I'm curious to know what didn't work and why as I think it would be good to learn/see.
If this is for debugging/development purposes, I can recommend console.log() and console.dir().
You may need to JSON.stringify() your array if it contains objects.