I've written some simple js to change visibility of some layers and the classes of some links, but the javascript only works the first time it's run... and help would be much appreciated.
The second time I click the link I get 'object doesn't support this property or method' on the line that calls the function?
if (TextLayerDisplay=='block'){
currentLayer="Text"
nextLayer="Img1"
}
if (Img1LayerDisplay=='block'){
currentLayer="Img1"
nextLayer="Img2"
}
if (Img2LayerDisplay=='block'){
currentLayer="Img2"
nextLayer="Img3"
}
if (Img3LayerDisplay=='block'){
currentLayer="Img3"
nextLayer="Img4"
}
if (Img4LayerDisplay=='block'){
currentLayer="Img4"
nextLayer="Img5"
}
if (Img5LayerDisplay=='block'){
currentLayer="Img5"
nextLayer="Img6"
}
if (Img6LayerDisplay=='block'){
currentLayer="Img6"
nextLayer="Text"
}
Is your HTML available also, so that I can run your code in my browser and see what it's doing? Or is your page available online somewhere that I could look at it and test it out in order to see if I can find the problem?
It's the >> link in the images menu that I'm having issues with, you can use all of the other links fine (1,2,3,4,5,6,info) and click the >> once, at any point, but the second time it's clicked is when I'm having the problem.
All that it's meant to do is change the layer (e.g. if Img1 display = block then Img1 display = none; Img2 display = block) & change the link class (If Img1 display = block then Link1 class=none; Link2 class=current)
Thanks for the link. First off, let me tell you that the more and more I look at the swapLayer function you have there, the more I am concerned that it isn't written properly. So let me ask you about how the content of that page is laid out. Are all of the images already a part of the page, and just some of them are hidden by the style display: none;. If that is not the case, can you tell me how the images are loaded? Just brainstorm with me for a minute.
Let's assume that all of your images are basically placed on your page like this:
Then what I would do is place a page state variable somewhere, say next to my function, displayNextImage. And then I'll have my function displayNextImage. Now I'll also know how many images I have on the page, in this case I have 3. So:
Code:
var currentImage = 1;
var numberOfImages = 3;
function displayNextImage() {
if(currentImage == numberOfImages) {
currentImage = 1;
} else {
currentImage++;
}
document.getElementById("image" + currentImage).className = "";
}
Now of course I want to also hide the previous images, so I create a method named hidePreviousImage like this:
Does that help simplify the process a little? Now if this is not your layout then all is not lost, you still want a global variable identifying the total number of images, one identifying the current image, and a function to display the next image and one to hide the previous image.
Last edited by nathandelane; 09-28-2009 at 11:19 AM.
Bookmarks