It should be possible, but will be made easier if the code is generated properly from the server-side. Store the second gallery image sources in arrays. When an image changes in the first gallery (onclick?), load the second gallery from the corresponding array. To do that, use another array to map the first gallery images to its set of images in the second gallery. To change images, change the src property of the images. There are other alternatives to this too, e.g. storing in XML and using JavaScript to parse the file.