I have an array.js file and an index.html.
My array.js file looks like this:
function go(){
var array = new Array();
array[0] = "Red";
array[1] = "Blue";
array[3] = "Green";
for (var i=0; i < array.length; i++){
document.write("<li>" + array[i] + "<br />");
}
}
My index.html file looks like this:
<html>
<head>
<script type="text/javascript" src="array.js"></script>
</head>
<body>
<input type="button" onclick="go()" value="Display JS Array"/>
<script>
go();
</script>
</body>
</html>
When I click the Display JS Array button on the HTML page, nothing happens.
I want the array elements to be displayed after I click the button.
It should look like:
- Red
- Blue
- Green
replace this
document.write("<li>" + array[i] + "<br />");withdocument.write("<li>" + array[i] + "</li>");and in your HTML file, remove
because already you calling go(); function on onclick. and in your array, array[2] will give undefined.
try this its working for me :