How to change background color multiple times with one button (only JavaScript please)? -


i'm kinda new javascript , i'm trying figure out how have 1 button changes background color on click. @ moment can 3 three separate buttons, don't know how one.

on click want next color in list selected.

i have no knowledge on jquery, appreciate if code isn't in jquery.

this have @ moment:

<button onclick="myfunction1()">red</button> <script type="text/javascript">function myfunction1() { document.body.style.backgroundcolor = "red"; }</script>  <button onclick="myfunction2()">blue</button> <script type="text/javascript">function myfunction2() { document.body.style.backgroundcolor = "blue"; }</script>  <button onclick="myfunction3()">green</button> <script type="text/javascript">function myfunction3() { document.body.style.backgroundcolor = "green"; }</script> 

const changecolor = document.getelementbyid('changecolor'),        colors      = ['red', 'green', 'blue'];  let   colorindex  = 0;    changecolor.addeventlistener('click', () => {    document.body.style.backgroundcolor = colors[colorindex];          colorindex = (colorindex + 1) % colors.length  });
<button id="changecolor">changecolor</button>


Comments

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

android - ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE -

c# - Populating Gridview inside Listview ItemTemplate On Web User Control from Code Behind -