Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q681 If I have an image object within <DIV> tags, how can I access the image without errors?

You are here: irt.org | FAQ | DHTML | Q681 [ previous next ]

If you've turned the <DIV> into a layer, then you'll have to go via the layer first:

<DIV NAME="myDiv" STYLE="position: relative">
<IMG SRC="image.gif" NAME="myImage" WIDTH="100" HEIGHT="100">
</DIV>

<FORM>
<INPUT TYPE="BUTTON" VALUE="Click Me!" onClick="if (document.images) document.layers['myDiv'].document.images['myImage'].src = 'another.gif'">
</FORM>

Instead of:

<DIV>
<IMG SRC="image.gif" NAME="myImage" WIDTH="100" HEIGHT="100">
</DIV>

<FORM>
<INPUT TYPE="BUTTON" VALUE="Click Me!" onClick="if (document.images) document.images['myImage'].src = 'another.gif'">
</FORM>

It is also necessary to test for the support of layers:

<DIV NAME="myDiv" STYLE="position: relative">
<IMG SRC="image.gif" NAME="myImage" WIDTH="100" HEIGHT="100">
</DIV>

<FORM>
<INPUT TYPE="BUTTON" VALUE="Click Me!" onClick="if (document.images) {if (document.layers) document.layers['myDiv'].document.images['myImage'].src = 'another.gif'; else document.images['myImage'].src}">
</FORM>

©2018 Martin Webb