6

I am still new to JavaScript. I need to create a button in html and allow the user to click the button to change the specify body background color.

var colors = ["purple", "yellow", "black"];

Above is the given array in the JavaScript. If the user click more than 3 times (after blue color), the green color will be selected again.

<form>
<p><input type="button" value="Change Color" name="B1" onclick="changeColor()"></p>
</form>

I roughly write the function for the button, but i did not know how i can assign the color from the array to the background color

function changeColor()
{   
    document.body.style.backgroundColor="colors[i]";
    i++
    if(i >=2)
    {
     i = 0;
    }
}

Hope someone can correct my code.

1
  • I'm guessing there's a var i = 0; somewhere outside changeColor. Next just use document.body.style.backgroundColor = colors[i];. Commented Aug 12, 2015 at 7:24

5 Answers 5

6

First you need to define global i variable, that will keep your current index. Than check if it's value extends array length, if so, than you should reset it, to the array start.

var colors = ["purple", "yellow", "black"];
var i = 0; // define global i variable
function changeColor()
{   
    document.body.style.backgroundColor = colors[i++];
    if(i >= colors.length)
    {
        i = 0;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

Little bit mistake in your logic,

Here is what you want.

var colors = ["purple", "yellow", "black"];
var i = 0;

function changeColor() {
  document.body.style.backgroundColor = colors[i];
  if (i >= 2) {
    i = 0;
    return;//Return without incresing it when it becomes 0 again.
  }
  i++;//Move increment to bottom of page.
}
<form>
  <p>
    <input type="button" value="Change Color" name="B1" onclick="changeColor()">
  </p>
</form>

2 Comments

Replace if (i >= 2) with array.length, hardcoding max iterator value is dangerous:)
@Beri ,You are right, Answer is just related to questions logic for better understanding to Firion Redfield.
3

This is probably the 'best' way to do it, although the code is more difficult to understand than the other answers:

var changeColor = (function(){
    var colors = ["purple", "yellow", "black"], i=0;    
    return function(){
        document.body.style.backgroundColor = colors[i++ % colors.length];
    };
}());

By declaring the i and colors variables inside an anonymous function you avoid creating global variables, and prevent any other code from accessing these values.

By using the % remainder operator with colors.length you can add/remove colors from the array without changing any other part of your code.

1 Comment

I understand that :), never thought to use modulo for the array size.
3

The simplest implementation:

var colors = ["purple", "yellow", "black"];
var i = 0;
function changeColor()
{   
    document.body.style.backgroundColor = colors[i];
    ++i;
    if(i >= colors.length)
    {
        i = 0;
    }
}

Comments

2

Don't put the array reference in quotes. Variables are not expanded inside quotes. So just assign the array reference to the style:

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

1 Comment

Thx :) it helpful for me

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.