2

I have a text array. I want to display the first entry on page load. And then replace the text with the next entry when I click a button. If I keep clicking the button I want the text to continuously be replaced by waht is next in the array, and when it gets to the end start back at the first entry. Can someone please show me an example code for that. I am new to this.

Here's what I have

$(document).ready(function(){
  var arr = new Array("One","Two","Three");
  var len=arr.length;
  $('#next').click(function(){ 
    for(var i=0; i<len; i++) { 
      $('#quote').html(arr[i]); 
    } 
 });
});
2
  • 2
    I will, if you show what you've tried Commented Oct 20, 2011 at 23:31
  • This is about the closest I can get. $(document).ready(function(){ var arr = new Array("One","Two","Three"); var len=arr.length; $('#next').click(function(){ for(var i=0; i<len; i++) { $('#quote').html(arr[i]); } }); }); Commented Oct 20, 2011 at 23:43

3 Answers 3

4

Something like the following should do the trick:

<script type="text/javascript">
var nextWord = (function() {
  var wordArray = ['fe','fi','fo','fum'];
  var count = -1;
  return function() {
    return wordArray[++count % wordArray.length];
  }
}());

</script>

<p id="foo">&nbsp;</p>

<button onclick="
  document.getElementById('foo').innerHTML = nextWord();
">Update</button>

Edit

Radomised version:

var nextWord = (function() {
  var wordArray = ['fe','fi','fo','fum'];
  var copy;
  return function() {
    if (!copy || !copy.length) copy = wordArray.slice();
    return copy.splice(Math.random() * copy.length | 0, 1);
  }
}());
Sign up to request clarification or add additional context in comments.

3 Comments

Is it possible to do it at random without repeat, unless you have seen everything?
Yes. Copy the array, use Math.random()*copy.length|0 to splice a member from the copy. When it's length is zero, make a new copy and start again. However, when the array is re-built the last word of the previous copy might be the first one selected from the new copy. The more words in the array, the less likely that is to happen. You could also remove the last word from the new copy so it can't repeat, but that will make the result less random.
Thanks everyone. This has been a real life safer. Seems so simple once you see how it is done!
3

The following should do it http://jsfiddle.net/mendesjuan/9jERn/1

$(document).ready(function(){
  var arr = ["One","Two","Three"];
  var index = 0;
  $('#next').click(function(){ 
    $('#quote').html(arr[index]); 
    index = (index + 1) % arr.length ;
 });
});

Your code was writing all three values each time you clicked it (but only displaying that last value)

2 Comments

Thanks a lot for the reply. When I get to "Three" though, can I click again and have it reset back to "One" and start over?
Thanks works like a charm nice work on modulo. Resets back on 0 when reaches array max length.
2

I think something like this would work

The javascript would look like:

// assuming maxTextArrayIndex & textArray are defined & populated
var textDisplayIndex = -1;
document.getElementById('textDisplay').innerHTML = textArray[textDisplayIndex];

function nextElement()
{
    textDisplayIndex += 1;
    if (textDisplayIndex > maxTextArrayIndex)
    {
        textDisplayIndex = 0;
    }

    document.getElementById('textDisplay').innerHTML = textArray[textDisplayIndex];
}

The html would look like:

<body onLoad=nextElement()>
...
<elementToDisplayText id=textDisplay></elementToDisplayText>
<button onClick=nextElement()>Next</button>

1 Comment

This works, but you didn't have to completely change the OP's code

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.