i'm new in JavaScript.
I have an array with some values and I'd like to show each array values within a list.
This is my script
<script>
var fruits = "apple,banana,watermelon,coconut";
var plits = fruits.split(',');
function a(){
for(i=0; i<plits.length; i++){
document.getElementById('output').innerHTML = "<li>" + plits[i] + " fruit</li>";
//document.write("<li>" + plits[i] + " fruit</li>");
}
}
</script>
<button onclick="a()">show fruits</button>
<div id='output'></div>
Whenever i run the code, it only shows the latest value of the array.
Likewise when i use the commented script above, the page changed and it turns infinity loading.
How can i show the array values within the #output?
document.getElementById('output').innerHTML += "<div>" + plits[i] + " fruit</div>";