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
    1. Advertising

  2. (Sybil) writes:

    > Could someone tell me what is wrong with this function?


    In which browser?

    > function imageNext(){


    > if ( document.part.src == "images/returnAdapter.gif"){


    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
    document.images['part']

    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)
    document.all.foo.src
    the result is
    "file:///D:/Home/lrn/html/PicA.png"
    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) {

    > document.part.src = "images/valveIntake.gif";


    Again, "document.imagesØ is safer.

    > document.getElementById("c").style.color = "black";
    > document.getElementById("d").style.color = "red";
    > }


    A "}" is missing here (just to be pedantic :)

    /L
    --
    Lasse Reichstein Nielsen -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Aug 26, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hadAnet
    Replies:
    2
    Views:
    4,335
    hadAnet
    Dec 28, 2005
  2. Glenn D

    swap image

    Glenn D, Mar 7, 2004, in forum: HTML
    Replies:
    1
    Views:
    472
    Glenn D
    Mar 7, 2004
  3. Phillip Roncoroni
    Replies:
    6
    Views:
    6,184
  4. Niels Dekker (no reply address)

    What swap is called when using std::swap?

    Niels Dekker (no reply address), Jul 19, 2006, in forum: C++
    Replies:
    4
    Views:
    970
    Niels Dekker (no reply address)
    Jul 20, 2006
  5. Clay
    Replies:
    1
    Views:
    135
    Elegie
    Sep 28, 2006
Loading...

Share This Page