Array next object

Posted on 2013-10-22
Medium Priority
Last Modified: 2013-11-19
Hello expert,

Thought I could get the next object in an array by
calling the array with [+1} next to it. But its not working.
How can I get the next array item. Full code below.


Allen in Dallas

<style type="text/css">

 body {font-family: arial;}
<script type="text/javascript">
function ChangeIt()
var colors;
colors = new Array("red", "blue", "green", "yellow", "purple");

document.body.style.backgroundColor = colors[0];

var t = setInterval(function() {
    document.body.style.backgroundColor = colors[+1];
}, 3000);

This page begins with a red background and
changes the body background after three seconds.

The Javascript
function is set in header section and called
from the body.
<script type="text/javascript">
Question by:9apit
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 53

Accepted Solution

COBOLdinosaur earned 2000 total points
ID: 39592768
That will get blue and continue to get blue forever.  

You need to set an index variable and increment it:
document.body.style.backgroundColor = colors[0];
var t = setInterval(function() {
    i=(i>=color.length) ? 0 : i+1;
    document.body.style.backgroundColor = colors[i];
}, 3000);

Open in new window


Author Comment

ID: 39593983
Hello COBOL dinasaur,

Thanks for the help.
I inserted your excellent script
into the page with no result.

I'll bet its just a syntax error but
I can't find it.


Allen in Dallas

Author Comment

ID: 39594014
Hello COBOL,

I found it
i=(i>=color.length) ? 0 : i+1;
should be
i=(i>=colors.length) ? 0 : i+1;

Works now



Author Closing Comment

ID: 39594022
Thanks for the excellent answer.

in the expression
 i=(i>=colors.length) ? 0 : i+1;

It says if I is greater than the length of the array then set i back to 1

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
The viewer will learn how to count occurrences of each item in an array.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question