Link to home
Create AccountLog in
Avatar of omnova
omnova

asked on

Javascript Array containing src line of pictures

I am trying to create an array using javascript that will contain the location of pictures. I then want to write a function that will be called by a button to increment a variable and the variable will refer to a position in the array and the img will change to new location stated in the array.

So far I have for the script and button:

<script type="text/javascript">
<!--
var slidearray= new Array("images/picspage/pic1.jpg","images/picspage/pic2.jpg","images/picspage/pic3.jpg","images/picspage/pic4.JPG","images/picspage/pic5.JPG",
"images/picspage/pic6.JPG","images/picspage/pic7.JPG","images/picspage/pic8.JPG","images/picspage/pic9.JPG","images/picspage/pic10.JPG","images/picspage/pic11.JPG");

var i=0;

function nextbutton(form)
{
if( i < slidearray.length-1)
{
i++;
document.slideshow.src=slidearray[i];
}
}

function previousbutton(form)
{
if(i>0)
{
i=i-1;
document.slideshow.src=slidearray[i];
}
}
//-->
</scirpt>



<form action="" >

<img src="images/picspage/pic1.jpg" name="slideshow" height="325px" width="325px" />;

<input type="button"
name="previous" value="Previous"
onclick="previousbutton(this.form)"
/>

<input type="button"
name="next" value="Next"
onclick="nextbutton(this.form)"
/>;

</form>



Also is there any way to tell if the array is actually being populated ??
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You need an onclick handler that does

index = (index + 1) % array.length;
img.src = array(index);
>> Also is there any way to tell if the array is actually being populated ??

You can check like..

if ( slidearray != null)
{
   //Yes array is populated..
}
else
{
//array is not populated..
}
Avatar of omnova
omnova

ASKER

cmalakar: Where would I insert the onclick handler and what is the code for that

I havent used scripting in a while and im trying to gather bits and pieces from old code I wrote a while ago.
Well that would have to be more like

if ( slidearray != null && slidearray(0) != null)
SOLUTION
Avatar of cmalakar
cmalakar
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
:-)