1

I want to step through an array with a next and previous button. Each button calls the same function as it is in. I have the following code...

    function displayPic(i) {
        if (i == 0) {
            $('#map_holder').empty();
            $('#map_holder').append(picArray[i] + '<br><input id="next" type="button" value="Next" onclick="displayPic(' + i + 1 + ');" />');

            if (i == picArray.length - 1) {
                $('#map_holder').empty();
                $('#map_holder').append(picArray[i] + '<br><input id="prev" type="button" value="Prev" onclick="displayPic(' + i - 1 + ');" />');
            }
            else {
                $('#map_holder').empty();
                $('#map_holder').append(picArray[i] + '<br><input id="prev" type="button" value="Prev" onclick="displayPic(' + i - 1 + ');" />&nbsp;&nbsp;&nbsp;&nbsp;<input id="next" type="button" value="Next" onclick="displayPic(' + i + 1 + ');" />');
            }
        }
    }

Any help?

2
  • Have a look at the generated HTML. It should look something like onclick="displayPic(01)" and onclick="displayPic(12)". Can you guess what you'd have to change? Also, displayPic won't do anything at all if i > 0. Commented May 8, 2015 at 2:00
  • @charlietfl: It doesn't look like they are. Commented May 8, 2015 at 2:14

2 Answers 2

2

I do not recommend adding button each time with the new image displayed, it makes it more complicated. How about using a global variable i and building the form early with <img> and <button id='prev'> and <button id='next'>?

<div id='map_holder'></div>//this will display the image
<button id="prev">Prev</button>
<button id="next">Next</button>

var i = 0;
displayPic(0); //show the first photo initially

$('#prev').click(function(){
    i--;
    displayPic(i);
});

$('#next').click(function(){
    i++;
    displayPic(i);
});

function displayPic(i) {        
    $('#map_holder').empty();
    $('#map_holder').append(picArray[i]);

    if(i == 0) 
        $('#prev').hide();
    else  
        $('#prev').show();     

    if(i == picArray.length-1)
        $('#next').hide();
    else
        $('#next').show();   
}

DEMO: https://jsfiddle.net/f4j884cs/1/

Sign up to request clarification or add additional context in comments.

2 Comments

You might want to explain why their code does not work, so they can learn from their mistakes.
I want to render new images and buttons each time. The first image doesn't have the previous button and the last image doesn't have a next button.
0

Looking at your code, there is an issue. The outer block checks if (i == 0) then the inner block checks if (i == picArray.length - 1). The inner block condition will never evaluate to true under normal circumstances because the value of i is guaranteed to be 0 due to the outer if (i == 0) condition.

Try this to start:

function displayPic(i) {
    if (i == 0) {
        $('#map_holder').empty();
        $('#map_holder').append(picArray[i] + '<br><input id="next" type="button" value="Next" onclick="displayPic(' + i + 1 + ');" />');
    }
    else if (i == picArray.length - 1) {
        $('#map_holder').empty();
        $('#map_holder').append(picArray[i] + '<br><input id="prev" type="button" value="Prev" onclick="displayPic(' + i - 1 + ');" />');
    }
    else {
        $('#map_holder').empty();
        $('#map_holder').append(picArray[i] + '<br><input id="prev" type="button" value="Prev" onclick="displayPic(' + i - 1 + ');" />&nbsp;&nbsp;&nbsp;&nbsp;<input id="next" type="button" value="Next" onclick="displayPic(' + i + 1 + ');" />');
    }
}

If this still doesn't work, please provide further details.

1 Comment

Yes I have tried that a couple of times. Provided below is the initializer when a "Display Picture" is clicked. It then generates an image and a next button and calls the displayPic function. $('#map_holder').append(picArray[0] + '<br><input id="next" type="button" value="Next" onclick="displayPic(1);" />');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.