1.) Don't use innerHTML , it's a proprietary Microsoft JScript method. No, because some low-brow decided to grandfather it in to a standard does not mean anyone should use it. XHTML and even regular HTML is not text, it's syntax. So when you later reference an element it may or may not be there because you may be referencing the DOM or you may be referencing nothing because what you thought was code was dumped as a bunch of text.

2.) Revised after you clarified your question.

Save as example.xhtml and make sure that you are using images with those variable names or you change the variable names in the code to the names of the images you are testing with.