HTML Radio Buttons Change Images
Let's do it again but swap two images this time.
The following snippets are from this page:
input statements in a
form should have the same
name and the id of the main image should correspond to this name. The
name/id should be different for each
form. In this example the name/id is a.
In the preLoad function each image is assigned a unique label. The first character of the label corresponds to the name/id used in the form. The input statements invoke function im passing the appropriate label. In function
im we concatenate ".src"
to the label and evaluate it to obtain the image path. Using the first character of the label (image) this path is assigned to the main image.
Labels such as
a2 serve a dual purpose. In addition to identifying the image path specified in the
preLoad function, the first letter of the name (
a) indicates the
id of the corresponding
img statement in the
body of the document. In other words the label establishes a relation between the path to the image and the appropriate
Allocating images and using the first character as an
id simplifies code in the body. Pre-allocating them in the preLoad function also has the advantage that images are fetched before the body executes. As a consequence they will display quickly when referenced. Setting
autocomplete="off" in the
form causes it to properly reset if the page is refreshed. I suggest you use your browser to view the source of this page.