...

View Full Version : Incrementing Arrays



mendesdusters
10-17-2005, 08:39 PM
I want to have plus and minus buttons that control image swapping. It seems like an easy enough concept of incrementing an array but I cannot get it to work. Any help is appreciated. The code is short and is given below.
_________________________________________________________________

<html>

<head>
<script LANGUAGE="JavaScript">

var help_array = new Array()
help_array[0] = "one.jpg"
help_array[1] = "two.jpg"
help_array[2] = "three.jpg"

var inc = 0;

function plus(intitial) {
document.prelim_setup_help.src = intitial
inc++
current = help_array[inc]
}

function plus(intitial) {
document.prelim_setup_help.src = intitial
current = help_array[inc]
inc--
current = help_array[inc]
}

</script>

</head>

<body>

<a href="javascript:plus(current);">plus</a>
<a href="javascript:minus(current);">minus</a>

<img src="one.jpg" name="prelim_setup_help"></img>

</body>
</html>

Philip M
10-17-2005, 09:01 PM
Here's what I use:-

<table>
<tr>
<td align="center" width="100%"><img src="lisbon1.jpg" name="photoslider"></td>
</tr>

<tr>
<td width="100%"><form method="POST" name="rotater">
<div align="center">

<SCRIPT language="JavaScript1.1">
<!--
var photos=new Array();
var which=0;

photos[0]="lisbon1.jpg"
photos[1]="lisbon2.jpg"
photos[2]="lisbon3.jpg"
photos[3]="lisbon4.jpg"
photos[4]="lisbon5.jpg"
photos[5]="lisbon6.jpg"
photos[6]="lisbon7.jpg" // and so on, as many as you like

function backward(){
if (which>0) {
window.status='';
which-- ;
document.images.photoslider.src=photos[which];
}
}

function forward(){
if (which<photos.length-1) {
which++ ;
document.images.photoslider.src=photos[which];
}
else window.status = 'End of gallery';
}
//-->
</SCRIPT>

<br>
<input type="button" value="&lt;&lt; Back" name="B2"
onClick="backward()"> &nbsp &nbsp <input type="button" value="Next &gt;&gt;" name="B1"
onClick="forward()"><br><br>
<a href="#" onClick="which=1;backward();return false"><font face="Arial, Helvetica" color="#0000FF"><small><b>Go back to the start</b></small></a></p>

</form>
</td>
</tr>
</table>


Hope this is useful.

mendesdusters
10-17-2005, 09:21 PM
Phillip M, thanks for the quick reply. I used the same concept as you and I still get an error. It is shown below. It seems like it should work.
_________________________________________________________________

<html>

<head>
<script LANGUAGE="JavaScript">

var help_array = new Array()
var which = 0;

help_array[0] = "one.jpg"
help_array[1] = "two.jpg"
help_array[2] = "three.jpg"

function plus() {
if (which<help_array.length-1){
which++;
document.prelim_setup_help.src = help_array(which);
}}

function minus() {
if (which>0){
which--;
document.prelim_setup_help.src = help_array(which);
}}

</script>

</head>

<body>

<a href="javascript:plus();">plus</a>
<a href="javascript:minus();">minus</a>

<img src="one.jpg" name="prelim_setup_help"></img>

</body>
</html>

greasonwolfe
10-17-2005, 09:44 PM
help_array[0] = new Image
help_array[1] = new Image
help_array[2] = new Image

help_array[0].src = "one.jpg"
help_array[1].src = "two.jpg"
help_array[2].src = "three.jpg"

function plus() {
if (which<help_array.length-1){
which++;
document.images.prelim_setup_help.src = eval(help_array[which]+".src");
}
}

function minus() {
if (which>0){
which--;
document.images.prelim_setup_help.src = eval(help_array[which]+".src");
}
}


I think this will do the trick for you. Have been sort of having the same problem myself.

greasonwolfe
10-17-2005, 09:53 PM
Ignore my last offering. Since you are saving space with the way you wrote the basic code, the changes in Red should do the trick.

function plus() {
if (which<help_array.length-1){
which++;
document.images.prelim_setup_help.src = help_array[which];
}}

function minus() {
if (which>0){
which--;
document.images.prelim_setup_help.src = help_array[which];
}}

mendesdusters
10-17-2005, 09:59 PM
Still doesn't work. I still get an error. Maybe I am doing something wrong? I don't understand why I am having such a difficult time with this. Code shown below.
________________________________________________________________

<html>

<head>
<script LANGUAGE="JavaScript">

var help_array = new Array()
var which = 0;

help_array[0] = new Image
help_array[1] = new Image
help_array[2] = new Image

help_array[0].src = "one.jpg"
help_array[1].src = "two.jpg"
help_array[2].src = "three.jpg"

function plus() {
if (which<help_array.length-1){
which++;
document.images.prelim_setup_help.src = eval(help_array[which+"src"]);
}
}


function minus() {
if (which>0){
which--;
document.images.prelim_setup_help.src = eval(help_array[which+"src"]);
}
}

</script>

</head>

<body>

<a href="javascript:plus();">plus</a>
<a href="javascript:minus();">minus</a>

<img src="one.jpg" name="prelim_setup_help"></img>

</body>
</html>

greasonwolfe
10-17-2005, 10:04 PM
Mendes, see my edits. So sorry about that, am working on little sleep here and wasn't paying attention to my color placement. Try the second one I gave you instead as it falls more in line with the original code you wrote. Basically, you need to add the "images." between the "document" and id in each statment and change the help_array(which) to help_array[which]

mendesdusters
10-17-2005, 10:13 PM
Greason, thanks for your help. A few changes to your code finally got the thing to work. It is below if you care at all.

<html>

<head>
<script LANGUAGE="JavaScript">

var help_array = new Array()
var inc = 0;

help_array[0] = new Image
help_array[1] = new Image
help_array[2] = new Image

help_array[0].src = "one.jpg"
help_array[1].src = "two.jpg"
help_array[2].src = "three.jpg"

function plus() {
if (inc<help_array.length-1){
inc++;
document.images.prelim_setup_help.src = help_array[inc].src;
}}


function minus() {
if (inc>0){
inc--;
document.images.prelim_setup_help.src = help_array[inc].src;
}}

</script>

</head>

<body>

<a href="javascript:plus();">plus</a>
<a href="javascript:minus();">minus</a>

<img src="one.jpg" name="prelim_setup_help"></img>

</body>
</html>

greasonwolfe
10-17-2005, 10:18 PM
No problem, glad I could at least be of some help. Maybe I will try sleeping tonight so the next time I do a better job of it. LOL



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum