#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Grosse Pointe Park Michigan
    Posts
    2
    Rep Power
    0

    Image change problem - javascript


    The following code was my attempt to take the image on SWITCH1 and have it replace the image on SW1. The image does in fact replace, but then reverts back to the original SW1 image. I place an alert in the code in an attempt to debug the process. Please, what am I doing wrong or what is a workaround?


    <HTML>
    <HEAD>
    <SCRIPT LANGUAGE="javaScript">
    function setValue(pos,val){
    $IM1 = new Image();
    $IM1.src="IMAGES/trcircle.gif";
    eval("document.SW"+pos+".src=$IM"+val+".src");
    alert(eval("document.SW"+pos+".src=$IM"+val+".src"));
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM NAME="TESTFORM">
    <TABLE>
    <TR><TD>SWITCH:</TD><TD ALIGN='CENTER'>
    <INPUT TYPE="IMAGE" NAME="SWITCH1" SRC="IMAGES/trcircle.gif" WIDTH="25" onClick="setValue(1,1);"></TD></TR>
    <TR><TD ALIGN='CENTER'>RESULT</TD><TD ALIGN='CENTER'>&nbsp;&nbsp;
    <A HREF=#><IMG NAME="SW1" SRC="" WIDTH="20" BORDER="0"></A></TD><TD></TR>
    </TABLE>
    </FORM>
    </BODY>
  2. #2
  3. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    what am I doing wrong...
    Hmmm....

    <INPUT TYPE="IMAGE"....

    this is the equivalent of <input type="submit"...
    form is being submitted, page is reloading

    function setValue(pos,val){
    $IM1 = new Image();
    $IM1.src="IMAGES/trcircle.gif";

    why a preloader inside a function?
    kind of defeats the purpose
    all you're using is the image src (a string)

    eval("document.SW"+pos+".src=$IM"+val+".src");

    don't use eval()! please...

    alert(eval("document.SW"+pos+".src=$IM"+val+".src"));

    were you trying to simply alert() the string?
    eval() will run it again. Like I said...

    <HTML>
    <HEAD>
    <SCRIPT LANGUAGE="javaScript">

    $IM1 = new Image();
    $IM1.src="IMAGES/trcircle.gif";

    function setImage(pos,val){
    document.images['SW' + pos].src = window['$IM' + val].src;
    }

    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM NAME="TESTFORM">
    <TABLE>
    <TR><TD>SWITCH:</TD><TD ALIGN='CENTER'>
    <img SRC="IMAGES/trcircle.gif" WIDTH="25" onClick="setImage(1,1);"></TD></TR>
    <TR><TD ALIGN='CENTER'>RESULT</TD><TD ALIGN='CENTER'>
    <A HREF=#><IMG NAME="SW1" SRC="" WIDTH="20" BORDER="0"></A></TD><TD></TR>
    </TABLE>
    </FORM>
    </BODY>
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Grosse Pointe Park Michigan
    Posts
    2
    Rep Power
    0

    Thank you Adios


    Thank you

IMN logo majestic logo threadwatch logo seochat tools logo