I am working on an optional exercise for a course, being optional no one has posted any helpful feedback on the course forum. I have finished the code as instructed, to the best of my ability and cannot located the problem.

The script is supposed to activate a red ball when clicked to travel around a national park map. Whenever the ball hits a landmark it is supposed to stop, a text description is supposed to appear as well as a photo in some cases. Once the ball makes it to the end of the map it is supposed to reload the page after a 7 second delay.

Below is my js file and my htm file. I have placed comments within the code to explain whenever possible what I have been asked to do. There are quite a few pictures for this so I won't post them unless asked.

I think this is a very interesting way to use JavaScript and would really like to get the program working. A lot of the content inside is a first for me, so be patient please when you run into an error and help me to correct my ways. Thanks to you!

Filename: trail.js

   Function List:
   placeIt(id, x, y)
      Places the id object at the coordinates (x,y)

      Hides the id object by setting its visibility style to "hidden"

      Hides the id object by setting its visibility style to "visible"


function placeIt(id, x, y) {

function showIt(id) {

function hideIt(id) {
   Filename:         trail.htm
   Supporting files: photo2.gif-photo8.gif, redball.gif, text1.gif-text9.gif,
                     trail.css, trail.gif, trail.js
<title>Heart of Rocks Trail</title>
<link href="trail.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="trail.js"> </script>
<script type="text/javascript">

   Trail Map

   Variable List:
      Contains an array of the x-coordinates of the trail map path
      Contains an array of the y-coordinates of the trail map path
var xArray = new Array(304,306,310,314,318,323,327,330,332,333,334,335,336,

var yArray = new Array(438,432,426,422,419,415,410,405,400,395,389,382,375,
var index = 1;
// hide all text and photo objects on the screen as the red ball travels around the trail
function hideAll() {
//display the text description and photo for a given landmark

/* since not every landmark has a photo use object detection to determine whether a div element with the value of the photo parameter exists and if it does call the showIt() function to display the photo */

function describeIt(text, photo) {
		if (document.getElementById("photo1")) { 
		showIt("photo1"); }
			if (document.getElementById("photo2")) {
			showIt("photo2"); }
				if (document.getElementById("photo3")) {
				showIt("photo3"); }
					if(document.getElementById("photo4")) {
						if(document.getElementById("photo5")) {
						showIt("photo5"); }
							if(document.getElementById("photo6")) {
							showIt("photo6"); }
								if(document.getElementById("photo7")) {
								showIt("photo7"); }
									if(document.getElementById("photo8")) {
									showIt("photo8") ; }
										showIt("text9"); {
										if (document.getElementById("photo9")) {
										showIt("photo9"); }	
// move the red ball along the hiking trail, stopping if the red ball encounters one of the landmarks
function runPath() {
	hideAll(); // call the hideAll() function to ensure that all text and photos are hidden on the page
	index ++;  // increase the value of the index variable by 1
	placeIt("redBall", xArray[index], yArray[index]) // place the redball at the xArray index, and yArray index
	switch(index) {
		case 0: 
		case 16: 
		describeIt("text2", "photo2");
		case 68: 
		describeIt("text3", "photo3");
		case 82: 
		describeIt("text4", "photo4");
		case 97: 
	 	describeIt("text5", "photo5");
		case 130: 
		describeIt("text6", "photo6");
		case 188: 
		describeIt("text7", "photo7");
		case 231: 
		describeIt("text8", "photo8");
		case 269: 
		describeIt("text9"); //call the describeIt() function with "text9"
		var index = 0; // set the value of the index variable to 0
		window.location.reload(7000); // reload the web page after a seven-second delay.
		// the default behaviour is to call the runPath() function agasin after a 10th of a second
		setTimeout("runPath()", 100); 


<div id="trail">
   <img src="trail.gif" width="650" height="490" alt="Heart of Rocks Trail" />

<!-- Mark the redBall.gif inline image as a link and set the href attricute of the link to run the runPath() function when the ball is clicked -->
<div id="redBall" onClick="runPath()">
     <img src="redball.gif"  alt="Click to follow trail" />

<div id="text1">
   <img src="text1.gif" alt="A Welcome Message" />

<div id="text2">
   <img src="text2.gif" alt="The Big Balanced Rock" />
<div id="photo2">
   <img src="photo2.jpg" width="200" height="155" alt="Big Balanced Rock" />

<div id="text3">
   <img src="text3.gif" alt="Duck on a Rock" />
<div id="photo3">
   <img src="photo3.jpg" width="200" height="155" alt="Duck on a Rock" />

<div id="text4">
   <img src="text4.gif" alt="The Camels Head" />
<div id="photo4">
   <img src="photo4.jpg" width="200" height="155" alt="Camels Head" />

<div id="text5">
   <img src="text5.gif" alt="Punch and Judy" />
<div id="photo5">
   <img src="photo5.jpg" width="200" height="155" alt="Punch and Judy" />

<div id="text6">
   <img src="text6.gif" alt="Thors Hammer" />
<div id="photo6">
   <img src="photo6.jpg" width="200" height="155" alt="Thors Hammer" />

<div id="text7">
   <img src="text7.gif" alt="The Pinnacle Balanced Rock" />
<div id="photo7">
   <img src="photo7.jpg" width="125" height="200" alt="Pinnacle Balanced Rock" />

<div id="text8">
   <img src="text8.gif" alt="The Balanced Rock" />
<div id="photo8">
   <img src="photo8.jpg" width="200" height="155" alt="Big Balanced Rock" />

<div id="text9">
   <img src="text9.gif" alt="A Short Exit Message" />


Good for you to try to learn this. There are many rooms in your code for improvement, but I am not going to tell you all right now because it could be too much.

Anyway, there are some immediate problem in the way you try.
1)Your trial image and ball image are not overlaid. As a result, it shows trial image first, and then the ball. What you may do is to use your div which holds the trial.jpg image as the real map. Give the div the trial.jpg background instead of place the image inside the div. Now, place your ball div inside the trial div. Finally, assign proper style properties to both of the div. A sample below shows you how to do it and the 'class' is the CSS part.

// CSS part
  .trial_bg {
    position: relative;
    background-image: url('trail.jpg');
    width: 650;
    height: 490;
  .the_ball {
    position: absolute;
    /* assign the starting poition (top-left of div) of the ball here */
    top: 0px;
    left: 0px;
    /* assign to the same size of the ball image */
    width: 16px;
    height: 16px;

// HTML part

  <div id="trail" class="trial_bg">
    <!-- Mark the redBall.gif inline image as a link and set the href attricute of the link to run the runPath() function when the ball is clicked -->
    <div id="redBall" class="the_ball" onClick="runPath()">
      <img src="redball.gif"  alt="Click to follow trail" />

2)You should hide all images when the page is loaded. You could simply assign the hidden property to all images. Also, use CSS in this case is safer when working on hiding and showing element because IE has had a bug with JavaScript when dynamically manipulating with 'visibility' property. Also, instead of having a div for each photo and text, why not having a div holding them inside? Doing so, you just need to change the hide/show property on only one 'div' instead of both? One more, you may consider the 'text' part to be just text which should be easier to work with. Below is the sample code.

// CSS part
  .show {
    /* actually no need anything here because properties are visible by default */
    /* when you switch from 'hide' to this one, it trigger default visibility */
  .hide {
    display: none;
    visibility: hidden;

// HTML part
<div id="location1" class="hide">
  <div id="text1">
    Here is the text for location 2

<div id="location2" class="hide">
  <div id="text2">
    Here is the text for location 2
  <div id="photo2">
    <img src="photo2.jpg" width="200" height="155" alt="Big Balanced Rock" />


3)If you give the hidden property to all descriptions by default, you don't need the hideAll() function. By the way, the way you call it should not be inside your runPath() but should be in the body tag as <body onload="hideAll()"> because you want all descriptions to be hidden when the page is loaded. Still, if you do the #2 above, you should not need this.

4)In your script part, you should always declare variable as local using 'var' keyword if the variable has a purpose of use only inside the function or scope. It could create confusion and unexpected result if you do not do that. By the way, as I mentioned earlier about using CSS rather than manipulate visibility style using Javascript, it will show up here. The reason is that IE will throw an error when you try to use JavaScript to change the visibility property of an element. And because you are doing so, you may need to move the code from your trial.js into the same location with other's script.

function placeIt(id, x, y) {
    var object=document.getElementById(id); // make 'object' variable local

  function showIt(id) {
    var object=document.getElementById(id); // make 'object' variable local
    object.className = "show";

  function hideIt(id) {
    var object=document.getElementById(id); // make 'object' variable local
    object.className = "hide";

Hmm... It gets too long here... Let me stop for now and wait for your response. :)

Thank you for all the helpful suggestions, being as new to coding as I am it is going to take me some time to sort through all of them.

The one problem I notice is that my text book has instructed us (more or less) or do the entire animation in JavaScript, it is a JavaScript course. I'm not arguing that this is the most efficient way, in fact I agree with you and trust your opinion. I'm wondering however how I might make this work following the instructions in the book?

If I create a new file and try it your way, will you try it the text book way? My comments are usually taken right from the text.

There are always many ways to do things, often too many :)

Hmm... Well, I could try that. However, you still have to overlay 'trail.gif' and 'redball.gif' still. The way you display will never overlay both images, and that will not create the animation you want. Therefore, you must choose one -- set trail.gif to be the div background image and put the redball.gif div inside the div OR set both trial.gif and redball.gif divs inside a div which has position relative, and set both gif's divs position to absolute.

I am still not sure about going through the arrayX & arrayY. I understand that you want to go through each point. However, the structure is not uniform and your script won't know when to stop at what point inside the array. You could either add another variable array to tell the script when to stop OR you redesign the structure of the array to be a path set for each location using hash-liked.