Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  97] [ 1]  / answers: 1 / hits: 90221  / 12 Years ago, tue, august 7, 2012, 12:00:00

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


More From » html

 Answers
21

replace this document.write(<li> + array[i] + <br />); with document.write(<li> + array[i] + </li>);



and in your HTML file, remove



<script>
go();
</script>


because already you calling go(); function on onclick. and in your array, array[2] will give undefined.



try this its working for me :



    <html>
<head>
<script type=text/javascript>
function go(){
var array = new Array();
array[0] = Red;
array[1] = Blue;
array[2] = Green;
li = document.getElementById(list);
li_arr = ;
for (var i=0; i < array.length; i++){
li_arr += <li> + array[i] + </li>;
}
li.innerHTML = li_arr;
}
</script>
</head>
<body>
<input type=button onclick=go() value=Display JS Array/>
<ul id=list></ul>
</body>
</html>



[#83805] Monday, August 6, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dominickmackenziet

Total Points: 583
Total Questions: 101
Total Answers: 117

Location: Saint Lucia
Member since Wed, Feb 8, 2023
1 Year ago
dominickmackenziet questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Fri, Feb 12, 21, 00:00, 3 Years ago
;