Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 1 decade ago

what is the smallest amount of code necessary for image swapping in java script?

what is the smallest amount of code necessary for image swapping in java script?

2 Answers

Relevance
  • John J
    Lv 6
    1 decade ago
    Favorite Answer

    function imageSwap(imgObj){

    imgObj.src="newimg.jpg";

    }

    you can also do this inline like -

    <img src="img1.jpg" onmouseover="this.src = 'img2.jpg';" />

  • Anonymous
    1 decade ago

    <SCRIPT LANGUAGE="JavaScript" type="text/javascript">

    <!--

    if(document.images) {

    pics = new Array();

    pics[1] = new Image();

    pics[1].src = "../images/b.jpg";

    pics[2] = new Image();

    pics[2].src = "../images/a.jpg";

    }

    function changer(from,to) {

    if(document.images) {

    document.images[from].src = pics[to].src;

    }

    }

    //-->

    </SCRIPT>

    #MouseOver Code

    <A onMouseOver="changer('img1',2)" onMouseOut="changer('img1',1)" href="javascript:void">

    <img name="img1"

    border="0" vspace="0" hspace="0" src="../images/b.jpg"></a>

    If you want to avoid writing the array everytime, this is an excellent resource - http://www.quirksmode.org/js/mouseov.html

Still have questions? Get your answers by asking now.