Closed Thread
Results 1 to 2 of 2

Thread: JS, functions, paramaters not passing

  1. #1
    zeroradius's Avatar
    zeroradius is offline Speaks fluent binary
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    1,403
    Rep Power
    25

    JS, functions, paramaters not passing

    Sorry for the onsalught of simple question I've been asking..... time for another. (as i mentioned in another thread this is my first time using JS for anything more complicated than alert())

    Ok so to my problem. I have a script that is supose to load the next element in an array when a button is clicked. I have tried putting the code in various places (header, body) I have tried sevral variations as well. By using a set of document.write() comands I found that it is not passing the paramaters to the picforward() and picbackward() functions. It confuses me because it was working and I just changed around the dunction names because they were backwards and it stopped working. I cant get it to work again no matter what i do (i also can't remember the code i had that worked......) I am passing the paramaters the way it tells me to in head first javascript but i just can't get it going again.

    Code:
     <script type="text/javascript">
    				var a=0;
    					function changepic(direction)
    					{
    						var direct = direction;
    						//document.write(a);
    						if(direct = "back")
    						{
    							//document.write(a);
    							a = picbackward(a);
    							return;
    						}
    						if(direct = "forward")
    						{
    							a = picforward(a);
    							return;
    						}
    						else
    						{
    							alert("A problem has occured");
    						}
    					}
    					
    					function picforward(k)
    					{
    						document.write(k);
    						var j = k;
    						j=j+1;
    						document.getElementById("myPic").src=pic[j];
    						return j;
    					}
    					function picbackward(k)
    					{
    						var j=k;
    						j=j-1;
    						document.getElementById("myPic").src=pic[j];
    						return j;
    					}
                </script>
    
                 <button name="backward" onClick=changepic("back")> Back</button>
                 <button name="forward" onClick=changepic("forward")> Forward </button>

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    kotg2 is offline Newbie
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Re: JS, functions, paramaters not passing

    You need to change the if statements so they are using the comparsion operator instead of the assignment operator, so
    "if(direct =" should be "if(direct ==".

    Also you need to add some additional code to ensure you don't stray outside the bounds of your array:

    function picforward(k)
    {
    document.write(k);
    var j = k;
    j=j+1;

    if(j <= 9)
    {
    document.getElementById("myPic").src=pic[j];
    return j;
    }
    return k;
    }
    function picbackward(k)
    {
    var j=k;
    j=j-1;

    if(j >= 0)
    {
    document.getElementById("myPic").src=pic[j];
    return j;
    }
    return k;
    }

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Passing variable
    By NumOne in forum PHP Development
    Replies: 1
    Last Post: 04-25-2011, 01:11 PM
  2. Replies: 4
    Last Post: 02-06-2011, 01:37 PM
  3. Javascript: Passing variables into functions question
    By atheium in forum JavaScript and CSS
    Replies: 7
    Last Post: 06-15-2010, 12:26 PM
  4. SQL Functions - SQL Encryption Functions
    By chili5 in forum Tutorials
    Replies: 8
    Last Post: 09-04-2009, 09:40 AM
  5. SQL Functions - Math Functions
    By chili5 in forum Tutorials
    Replies: 6
    Last Post: 09-02-2009, 02:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

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