Link to home
Start Free TrialLog in
Avatar of SETP
SETP

asked on

JavaScript Error?

Im not sure why the following code produces an error, when clicking on the hyperlink more than once?
Basically i create a 2-Dimen Array (5X5), then show the selected Index on load (which by default is 0) and below, i create hyperlinks to the remainding options... (see code below)

<html>
<head>
<title></title>
<script type="text/javascript">

var arr

function createArr()
{
   rows = 5;
   arr = new Array(rows)
   for (i=0;i<=(rows -1);i++)
   {
      column = 5;
      arr[i] = new Array(column)
      for (j=0;j<=(column -1);j++)
      {      
         arr[i][j] = i + "" + j
      }
   }
}

function showItem(item)
{
   count = arr[item].length
   var i=0
   for (i=0;i<=(count-1);i++)
   {
      document.write(arr[item][i] + "<br />")
   }
   document.write("----------- <br />")
   showList(item)
}

function showList(ignore)
{
   count = arr.length
   for (i=0;i<=(count -1);i++)
   {
      if (i == ignore)
      {
         document.write(arr[i][0])
      }
      else
      {
         document.write('<a href="javascript:showItem(' + i + ')">')
          document.write(arr[i][0])
          document.write('</a>')
      }
   document.write('<br />')
   }
}

createArr()
showItem(0)

</script>
</head>
<body>
</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gjutras
gjutras

iin your exisiting code as soon as you do a second showItem, the javascript disappears because your "page" is now all dynamically created. so the second call is trying to call a javascript function that doesn't exist anymore.
move everything except
showItem(0)
 into an external .js file
put in a script tag to include the js file.
put createArr() as the first line of the showItem() function
in the showItem() function put a writeln that also is a copy of that script include tag.