CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2008
    Posts
    31

    JavaScript newbie: - - decrement problem

    Hi everyone,

    I'm new to JS,and am having a little trouble with my script. It's a picture slide show.

    Basically, when I click 'Previous' to call the 'moveToPreviousSlide' function, on the first call, it increments instead of decrements.

    Say I'm currently viewing image 3, when I then click 'previous', it keeps going to image 4 instead, and then goes back down to 3, and then 2, instead of going straight to 2.





    Here is what I've made so far:




    Code:
    <script type="text/javascript">
    	var index = 0;
    	var titles = [1,2,3,4,5,6];
    
    
    	function moveToNextSlide()
    		{
    			if(index > 5)
    				{
    					index = 0;
    				}
    			var slideName = "images/slideshow/template" + titles[index++] + ".jpg";
    			var img1 = document.getElementById("img1");
    			img1.src = slideName;
    		}
    		
    	function moveToPreviousSlide()//on first call still increments by 1
    		{
    
    			var slideName = "images/slideshow/template" + titles[index--] + ".jpg";
    			var img1 = document.getElementById("img1");
    			img1.src = slideName;
    		}		
    
    
    	moveToNextSlide();
    </script>
    If anyone can tell me how to correct this I'd be very grateful!!

    Many thanks!

    Swerve
    Last edited by Swerve; December 1st, 2008 at 12:09 PM.

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: JavaScript newbie: - - decrement problem

    You're using the post-increment/decrement operator while you probably want to use the pre-increment/decrement operator.

    - petter

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured