0

I have setup an iframe on my page and would like it to update when clicking on a next button.

I have an array of urls which I want to be called when the next button is clicked.

The next button should load the next url in the iframe.

I am stuck at how do i always call the next index position each time i click the next button?

1
  • If my answer below has helped you, please be sure to click the checkmark to the left of it so it's green, marking it as the answer. Commented Sep 16, 2011 at 15:28

1 Answer 1

1
var pos = 0;
var urlArray = [...]; //Put your array of URLs here
$('#next').click(function() {
    if (pos == urlArray.length)
        pos = -1; //Reset it back to the beginning so it'll loop and not throw an exception.
    $('#youriframe').attr('src', urlArray[++pos]); //note the pre increment (++)
});
Sign up to request clarification or add additional context in comments.

1 Comment

This solution worked well for me. I'm now adding to the if statement so that the position decrements when clicking on previous.

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.