Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use Increment Arrays Properly?

Status
Not open for further replies.

mendesdusters

Programmer
May 17, 2005
2
US
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="javascriptlus(current);">plus</a>
<a href="javascript:minus(current);">minus</a>

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

</body>
</html>
 
You need to define [tt]current[/tt] as a global variable.

You have two functions named [tt]plus[/tt] and none named [tt]minus[/tt]

[tt]javascriptlus(current)[/tt] should read: [tt]javascript:plus(current)[/tt]

And even if you did manage to get one of your functions called, it looks as though they set the image source to what it is already, then set the image to swap to next time.

You also need some error checking to make sure the user doesn't overstep the bounds of the array.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top