Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Incrementing Arrays

    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>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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.
    Last edited by Philip M; 10-17-2005 at 09:09 PM.

  • #3
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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="javascriptlus();">plus</a>
    <a href="javascript:minus();">minus</a>

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

    </body>
    </html>

  • #4
    Regular Coder
    Join Date
    Nov 2004
    Location
    near the only active volcano in the continental US
    Posts
    409
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Code:
    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.
    Last edited by greasonwolfe; 10-17-2005 at 09:46 PM.

  • #5
    Regular Coder
    Join Date
    Nov 2004
    Location
    near the only active volcano in the continental US
    Posts
    409
    Thanks
    6
    Thanked 2 Times in 2 Posts
    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];
    }}
    Last edited by greasonwolfe; 10-17-2005 at 09:55 PM.

  • #6
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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="javascriptlus();">plus</a>
    <a href="javascript:minus();">minus</a>

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

    </body>
    </html>

  • #7
    Regular Coder
    Join Date
    Nov 2004
    Location
    near the only active volcano in the continental US
    Posts
    409
    Thanks
    6
    Thanked 2 Times in 2 Posts
    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]

  • #8
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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="javascriptlus();">plus</a>
    <a href="javascript:minus();">minus</a>

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

    </body>
    </html>

  • #9
    Regular Coder
    Join Date
    Nov 2004
    Location
    near the only active volcano in the continental US
    Posts
    409
    Thanks
    6
    Thanked 2 Times in 2 Posts
    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


  •  

    Posting Permissions

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