Nickool
Nickool

Reputation: 3702

Toggling between two images by clicking

Due to this link I changed it to this one:

  <html>
      <head>
    <script>

    var toggleimage=new Array("p1.gif","p.gif")

    //do not edit the variables below
    var image_1=new Image()
    var image_2=new Image()
    image_1.src=toggleimage[0]
    image_2.src=toggleimage[1]
    var i_image=0

    function testloading() {
        isloaded=true
    }

    function toggle() {
        if (isloaded) {
            document.togglepicture.src=toggleimage[i_image]
        }
        i_image++
        if (i_image>1) {i_image=0}
    }
    onload=testloading
    </script>

        <title>
    </title>
        <meta content="">
        <style></style>
      </head>
      <body>
    <a href="javascript:toggle()"><img name="togglepicture" src="p1.gif" border="0"></a>

    </body>
    </html>

when I click on the image p it will show me p1 and vice versa Now I have problem image has a name:

 <a href="javascript:toggle()"><img name="togglepicture" src="p1.gif" border="0"></a>

and it will get the name here:

document.togglepicture.src=toggleimage[i_image]

I want to have many images so I thaught I need to change the togglepicture to a variable for example:

function toggle(a) {
    if (isloaded) {
        document.a.src=toggleimage[i_image]
    }
    i_image++
    if (i_image>1) {i_image=0}
}

and for input forexample it will be toggle('nameofimage') and in the href it will be something like

 <a href="javascript:toggle('pic1')">

I wasn't successful.How can I use this function when I have more than a picture to click?

Upvotes: 3

Views: 9046

Answers (5)

Andrew D.
Andrew D.

Reputation: 8220

<html>
<head>
<script>
  // images list (properties name must by equal to id of IMAGE html element)
  var imageList={
    image1: {
      currentIndex:0,
      images:["p1.gif","p.gif"]
    }
  };
  // preloading images using closure (additionaly replace image URL's with image objects)
  (function() {
    for(var p in imageList)if(imageList.hasOwnProperty(p)) {
      for(var i=0;i<imageList[p].images.length;i++) {
        var img=new Image(),src=imageList[p].images[i];
        (imageList[p].images[i]=img).src=src;
      }
    }
  })();

  function toogleImage() {
    var info=imageList[this.id];
    info.currentIndex++;
    if(info.currentIndex===info.images.length)info.currentIndex=0;
    this.src=info.images[info.currentIndex].src;
  }

  // setting start images
  window.onload=function() {
    for(var p in imageList)if(imageList.hasOwnProperty(p)) {
      //try{ 
        document.getElementById(p).src=imageList[p].images[0].src;
      //}
      //catch(ex){}
    }
  }
</script>
</head>
<body>
  <img id="image1" onclick="toogleImage.call(this);"/>
</body>
</html>

http://jsfiddle.net/X4Q2m/

Upvotes: 0

user180100
user180100

Reputation:

I made a modular toogle, visible here: http://jsfiddle.net/Regisc/N7bgz/2/

Usage sample:

<img id="image1" src="http://dummyimage.com/50/f00/fff&text=a" 
     onclick='toogle(this, ["http://dummyimage.com/50/ab0/fff&text=b",
     "http://dummyimage.com/50/ab0/fff&text=c"]);' />

Upvotes: 2

Ayush
Ayush

Reputation: 42450

I'm not entirely sure I got your question. Are you asking:

  • How to edit the function to allow toggling between more than just two images, or
  • How to edit the function to handle more than one set of toggle images?

Toggling between more than just two images

var toggleimage=new Array("p1.gif","p.gif","p2.gif","p3.gif","p4.gif")
var totalImages=4;

function toggle() {
        if (isloaded) {
            document.togglepicture.src=toggleimage[i_image]
        }
        i_image++
        if (i_image>totalImages) {i_image=0}
    }

How to edit the function to handle more than one set of toggle images?

call the JS function like this

<a href="javascript:toggle(this)">

And, in your JS function,

var id = div.id;

Use this in an if-else to determine which control called the function and accordingly which array of images to use.

function toggle(div) 
{   
    var id = div.id;
    if (isloaded) 
    {
        if (id == 'myFirstToggleImageControl')
        {
           document.togglepicture.src=toggleimage[i_image];
        }
        else if (id == 'mySecondToggleImageControl')
        {
            document.togglepicture.src=toggleimageSource2[i_image];
        } 

    }
    i_image++
    if (i_image>1) {i_image=0}
}

Note: You will want to use an independent counter for the second control. So, possibly i_image1 and i_image2

Upvotes: 0

RobG
RobG

Reputation: 147513

Something like the following should work for any number of images provided toggleimage is a contiguous array.

var toggleimage = ["p1.gif","p.gif"];


var toggle = (function() {

    var count = 0;
    var len = toggleimage.length;
    var el = document.getElementsByName('togglepicture')[0]

    return function() {

      if (isloaded) {
        el.src = toggleimage[count++ % len];
      }
   };
}());

Upvotes: 0

Ankur
Ankur

Reputation: 12774

you can't use

document.a.src=toggleimage[i_image];

instead use

document.getElementById(a).src=toggleimage[i_image];

And you also need to add an id to your img element.

Upvotes: 0

Related Questions