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

Array next object

Posted on 2013-10-22
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
  • 3
LVL 53

Accepted Solution

COBOLdinosaur earned 500 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…

791 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