954,566 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Plz check my JS code and tell me why its not working ...

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

new_developer
Newbie Poster
22 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Maybe your myImages[i] is missing the file extension?

Javvy
Junior Poster
110 posts since Feb 2011
Reputation Points: 11
Solved Threads: 21
 
Maybe your myImages[i] is missing the file extension?

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

new_developer
Newbie Poster
22 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

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.

Javvy
Junior Poster
110 posts since Feb 2011
Reputation Points: 11
Solved Threads: 21
 

thank u ...

new_developer
Newbie Poster
22 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

You're welcome :)

Javvy
Junior Poster
110 posts since Feb 2011
Reputation Points: 11
Solved Threads: 21
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: