Hi everyone,
My javascript code is absolutely ok but its not working. I know it sounds funny but its happening with me.

Please view below code and tell me is there any mistake in it.


Javascript code:
Which change image when i click on button.

var myImages = new Array("images/ba1", "images/ba2", "images/ba3", "images/ba4", "images/ba5");

var i = 0;

function ChangeImage()
{
	i++;
	if(i == myImages.length)
	{
		i = 0;
	}
	
	document.getElementById("main").src = myImages[i]
}

Html code:
First div contains thumbnails. Second div contains large image of thumbnail. I want to change large image through javascript function.

<div>
<img src="images/a1.jpg" id="a1"/> -
<img src="images/a2.jpg" id="a2"/> -
<img src="images/a3.jpg" id="a3"/> -
<img src="images/a4.jpg" id="a4"/> -
<img src="images/a5.jpg" id="a5"/>
</div>

<input type="button" value="Next" onclick="ChangeImage()"/>

<div><img src="images/ba1.jpg" id="main"/></div>

Please anyone check my code and tell me whats the problem. It seems ok, but its not working. Images path is ok. I have to give assignment ...

Maybe your myImages is missing the file extension?

Maybe your myImages is missing the file extension?

I didn't understand ...
What is the relation of myImages and file extension ??

Care to try this..?

<script>
var myImages = new Array("<img src='images/ba1.jpg'>", "<img src='images/ba2.jpg'>", "<img src='images/ba3.jpg'>", "<img src='images/ba4.jpg'>", "<img src='images/ba5.jpg'>");

var i = 0;

function ChangeImage()
{
	i++;
	if(i == myImages.length)
	{
		i = 0;
	}
	
	document.getElementById("main").innerHTML = myImages[i]
}
</script>

<div>
<img src="images/a1.jpg" id="a1"/> -
<img src="images/a2.jpg" id="a2"/> -
<img src="images/a3.jpg" id="a3"/> -
<img src="images/a4.jpg" id="a4"/> -
<img src="images/a5.jpg" id="a5"/>
</div>

<input type="button" value="Next" onclick="ChangeImage()"/>

<div id="main"><img src="images/ba1.jpg"></div>

Changes in line 2, 14 and 18.

You're welcome :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.