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>
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;
}
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks