0

I've tried to use one single button to change background color of a div but failed. could anyone help me please.

var colourBox = ["Red", "blue", "green", "yellow", "purple", "pink"];
    
for(var c = 0; c < 6; c++;) {
    $(".btns1").click(clorChange);
    function clorChange() {
        $(".top_div").css('background-color', colourBox[c]);
        alert(colourBox[c]);
    }
}
DarkBee
  • 16,592
  • 6
  • 46
  • 58
sanz
  • 1
  • 2
    define 'failed' - what errors are you seeing, and you're probably best at posting up your HTML too to see how you are using this javascript – andy mccullough Oct 27 '20 at 11:53
  • 2
    I would suggest you to read more about functions, loops and events, and how and when to use each :) – MorKadosh Oct 27 '20 at 11:53
  • You should really open your developer console as the snippet contains syntax errors to start with – DarkBee Oct 27 '20 at 12:01
  • By the looks of you're code you want to change a div's background-color to your array of defined colors one by one. If that's the case, you wrote it wrong. – Saud Oct 27 '20 at 12:05

0 Answers0