Image Swap

Discussion in 'Javascript' started by Sybil, Aug 26, 2003.

  1. Sybil

    Sybil Guest

    Could someone tell me what is wrong with this function?
    function imageNext(){

    if ( document.part.src == "images/returnAdapter.gif"){
    document.part.src = "images/valveIntake.gif";
    document.getElementById("c").style.color = "black";
    document.getElementById("d").style.color = "red";

    The image name attribute is "part". But the image is not changing.

    Thanks, Sybil
    Sybil, Aug 26, 2003
    1. Advertisements

  2. In which browser?
    The assumption that "document.part" refers to the element with name
    "part", doesn't hold in all browsers. A safer way of referencing the
    element is

    You compare the value of the src property to a releative path.
    Most browsers (including IE6) changes to property to the absolute path.
    E.g., if I create an image as:
    <img src="../../PicA.png" id="foo">
    and then read the src value again with javascript (in IE 6)
    the result is
    That means that your comparison will alway fail.
    You should test that the src *ends* with your string, e.g., with a
    regular expression:

    if (/images\/returnAdapter.gid$/.test(document.images['part'].src) {
    Again, "document.imagesØ is safer.
    A "}" is missing here (just to be pedantic :)

    Lasse Reichstein Nielsen, Aug 26, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.